locked
display value as checkedin checkbox from [] RRS feed

  • Question

  • User-323149085 posted

    Hello ,

     I have a [] with 4 parmeters and I would like to display the db parameter and alow the user to select anther one (edit mode) 

    how ever , the display as select is always the last parameter in the [] 

    I think cod e will explain better 

    in my controller : 

    ViewBag.dis = new string[4] { "a", "b", "c", "d" };

    in my view 

      @{var disss = "b"; } //value just to be clear 
                                        @foreach (var item in (ViewBag.dis))
                                        {
                                            if (item == disss)
                                            {
                                                <li class="radio space">
                                                    <label>
                                                        <input type="radio" asp-for="District" value=@item checked="checked">
                                                      @item  <i class="fas fa-street-view"></i>
                                                    </label>
                                                </li>
                                            }
                                            //  if (item != Model.District)
                                            if (item !=disss)
                                            
                                            {
                                                <li class="radio space">
                                                    <label>
                                                        <input type="radio" asp-for="District" value=@item checked="">
                                                        @item <i class="fas fa-street-view"></i>
                                                    </label>
                                                </li>
                                            }

    what ever the value is the user always view value  d as checked how can I show the correct value as checked? Thanks 

    Thursday, August 1, 2019 12:13 PM

Answers

  • User-474980206 posted
    In html the checked attribute does not require a value and it value is meaningless. If the attribute exists then the value is checked. like the example above don’t include the checked attribute if you don’t want it checked.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 1, 2019 2:04 PM

All replies

  • User-194957375 posted
            <div>
    @foreach (var item in (ViewBag.dis))
    {
    <div class="form-check">
    <input class="form-check-input" type="radio" name="exampleRadios" id='exampleRadios@(item)' value="@item" @(item == disss ? "checked" : "") onchange="alert('@item');">
    <label class="form-check-label" for="exampleRadios@(item)">
    @item <i class="fas fa-street-view"></i>
    </label>
    </div>
    }
    </div>

    i use bootstrap

    Thursday, August 1, 2019 12:45 PM
  • User-474980206 posted
    In html the checked attribute does not require a value and it value is meaningless. If the attribute exists then the value is checked. like the example above don’t include the checked attribute if you don’t want it checked.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 1, 2019 2:04 PM
  • User-323149085 posted

    Thank you bruce 

    Thursday, August 1, 2019 4:07 PM