How to Set RadioButtonFor() in ASp.net MVC 2 as Checked by default

前端 未结 14 1873
一向
一向 2021-02-06 21:20

How can i Set RadioButtonFor() as Checked By Default

<%=Html.RadioButtonFor(m => m.Gender,\"Male\")%>

there is way out for (Html.Radio

14条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-06 21:44

    Came across this and thought I would point out that for MVC 5 all you need to do is set the value on the model. For Example:

    Model:

       public class ExampleModel
        {
            public PackingListInputModel()
            {
    
                 RadioButtonField = "One";
    
            }
            public string RadioButtonField { get; set; }
        }
    

    View :

    @model ExampleModel
    
    @using (Html.BeginForm)
    {
        @Html.RadioButtonFor(m => m.RadioButtonField , "One")
        @Html.RadioButtonFor(m => m.RadioButtonField , "Two")
    }
    

    The state of the first radio button ("One") will be set as active because the value matches what was set in the model.

提交回复
热议问题