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

前端 未结 14 1839
一向
一向 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:41

    You need to add 'checked' htmlAttribute in RadioButtonFor, if the radiobutton's value matches with Model.Gender value.

    @{
            foreach (var item in Model.GenderList)
            {
                <div class="btn-group" role="group">
                   <label class="btn btn-default">
                       @Html.RadioButtonFor(m => m.Gender, item.Key, (int)Model.Gender==item.Key ? new { @checked = "checked" } : null)
                       @item.Value
                  </label>
                </div>
            }
        }
    

    For complete code see below link: To render bootstrap radio button group with default checked. stackoverflow answer link

    0 讨论(0)
  • 2021-02-06 21:42

    You can also add labels that are tied to your radio buttons with the same ID, which then allows the user to click the radio button or label to select that item. I'm using constants here for "Male", "Female" and "Unknown", but obviously these could be strings in your model.

    <%: Html.RadioButtonFor(m => m.Gender, "Male", 
        new Dictionary<string, object> { { "checked", "checked" }, { "id", "Male" } }) %>
    <%: Html.Label("Male") %>
    
    <%: Html.RadioButtonFor(m => m.Gender, "Female", 
        new Dictionary<string, object> { { "id", "Female" } }) %>
    <%: Html.Label("Female")%>
    
    <%: Html.RadioButtonFor(m => m.Gender, "Unknown",
        new Dictionary<string, object> { { "id", "Unknown" } }) %>
    <%: Html.Label("Unknown")%>
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-06 21:44
               @Html.RadioButton("Insured.GenderType", 1, (Model.Insured.GenderType == 1 ))
               @Web.Mvc.Claims.Resources.PartyResource.MaleLabel
               @Html.RadioButton("Insured.GenderType", 2, Model.Insured.GenderType == 2)
               @Web.Mvc.Claims.Resources.PartyResource.FemaleLabel
    
    0 讨论(0)
  • 2021-02-06 21:45

    Here is code to set default radio button set to true

    @Html.RadioButtonFor(m => m.Gender, "Male", new { @checked = "checked", id = "rdGender", name = "rbGender" })
    
    0 讨论(0)
  • 2021-02-06 21:50

    I find it best to just put the default value in the constructor method of the model.

    Gender = "Male";
    
    0 讨论(0)
提交回复
热议问题