Set @Html.RadioButtonFor as Checked by default

21,125

Solution 1

Have a look here RadioButtonFor in ASP.NET MVC 2 or for list Has anyone implement RadioButtonListFor<T> for ASP.NET MVC?

If you are using an Enum see this answer pass enum to html.radiobuttonfor MVC3

Solution 2

In your controller action set the value of the UserType property on your view model to the corresponding value:

public ActionResult SomeAction()
{
    MyViewModel model = ...

    // preselect the second radio button
    model.UserType = "type2";
    return View(model);
}

and in your view:

<div id="private-user">
    @Html.RadioButtonFor(m => m.UserType, "type1", new { @class="type-radio" }) 1
</div>
<div id="proff-user">
    @Html.RadioButtonFor(m => m.UserType, "type2", new { @class="type-radio" }) 2
</div>
Share:
21,125
minchiya
Author by

minchiya

Updated on August 16, 2020

Comments

  • minchiya
    minchiya almost 4 years

    I am not able to set the default radio button to "Checked" ! I am using @Html.RadioButtonFor :

    <div id="private-user">
        @Html.RadioButtonFor(m => m.UserType, "type1", new { @class="type-radio" , **@Checked="checked"** }) 1
    </div>
    <div id="proff-user">
        @Html.RadioButtonFor(m => m.UserType, "type2", new { @class="type-radio" }) 2
    </div>
    

    Is it possible to set a radio button as cheched using @Html.RadioButtonFor ?

    Thx