Default radioobuttonlist in ASP.NET MVC Razor

How to ensure that the first option is selected in the switch list? If it is based on a model property, how is it set, if this is the first page that appears when the application starts?

It is easy peasy in webforms, but I don’t know how to do it in MVC, and it seems that in NET there is nowhere that shows how to do it.

This does not work:

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

I just turn off both radio buttons

+3
source share
3 answers
@Html.RadioButtonFor(x => x.Something, "radioValue", new { @checked = true })
+12
source
@Html.RadioButtonFor(x => x.Something, "radioValue", new { @checked = "checked"})

or if you are attached to a model, then

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

if we try something like this, where x.checked is a database binding. regardless of truth or falsehood. the last radio button in the list is marked, the rest of them are not marked.

?

+5

:

@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)
+3

Source: https://habr.com/ru/post/1796540/


All Articles