Default radiobuttonlist in ASP.NET MVC Razor

20,815

Solution 1

@Html.RadioButtonFor(x => x.Something, "radioValue", new { @checked = true })

Solution 2

@Html.RadioButtonFor(x => x.Something, "radioValue", new { @checked = "checked"})

or if you are binding from model then

 @Html.RadioButtonFor(x => x.Something, "radioValue", new { @checked = Model.checked})

if we try something like this where x.checked is binding from the database. irrespective of true or false. last radio-button in the list is checked, rest of them are unchecked.

how can we bind the Radio button list from Database and have it worked correctly?

Solution 3

This was my solution:

@Html.RadioButtonFor(model => model.Coin , "Dollar", Model.Coin == "$" ? (new { @checked = "checked" }) : null)
@Html.RadioButtonFor(model => model.Coin , "Euro", Model.Coin == "E" ? (new { @checked = "checked" }) : null)
Share:
20,815
jaffa
Author by

jaffa

Updated on April 11, 2020

Comments

  • jaffa
    jaffa about 4 years

    How do I ensure that a radio button list has the first option selected? If it is based on the model property, how is this set if this is the first page that is shown on application start-up?

    This is easy peasy in webforms but I don't know how to do this in MVC and there doesn't seem to be anywhere on the NET which shows how this is done.

    This doesn't seem to work:

    @Html.RadioButtonFor(m => m.SearchType, true) Location
    @Html.RadioButtonFor(m => m.SearchType, false) Name
    

    I Just get both radio buttons unselected??