44

我如何将 RadioButtonFor() 设置为默认选中

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

(Html.RadioButton) 有出路,但 (Html.RadioButtonFor) 没有出路

有任何想法吗?

4

14 回答 14

82

Use the simple way:

<%= Html.RadioButtonFor(m => m.Gender, "Male", new { Checked = "checked" })%>
于 2010-08-27T16:46:14.447 回答
16

It's not too pretty, but if you have to implement only very few radio buttons for the entire site, something like this might also be an option:

<%=Html.RadioButtonFor(m => m.Gender,"Male",Model.Gender=="Male" ? new { @checked = "checked" } : null)%>

于 2010-08-24T15:12:44.887 回答
14

我假设您应该有一组单选按钮。可能是这样的

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

您可以从您的控制器中为 m.Gender = "Unknown" (或其他东西)提供默认值。

于 2010-04-09T13:42:43.337 回答
12

StackOverflow 上的这个问题涉及 RadioButtonListFor 并且答案也解决了您的问题。您可以在 RadioButtonListViewModel 中设置 selected 属性。

于 2010-04-11T21:38:56.473 回答
8

This Helper evaluates the expression and if equals to the value it checks the radio button, and has the same parameters than RadioButtonFor (for this reason the name is diferent):

public static MvcHtmlString CheckedRadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value)
{
    return CheckedRadioButtonFor(htmlHelper, expression, value, null);
}

public static MvcHtmlString CheckedRadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, object htmlAttributes)
{
    var func = expression.Compile();
    var attributes = new RouteValueDictionary(htmlAttributes);
    if ((object)func(htmlHelper.ViewData.Model) == value) {
        attributes["checked"] = "checked";
    }
    return htmlHelper.RadioButtonFor(expression, value, attributes);
}

Usage:

<%= Html.CheckedRadioButtonFor(m => m.Gender, "Male", new { id = "gender-male" })%>

Result:

<!-- For Model.Gender = "Male" -->
<input checked="checked" id="gender-male" name="Gender" type="radio" value="Male">
<!-- For Model.Gender = "Female" -->
<input id="gender-male" name="Gender" type="radio" value="Male">
于 2010-09-24T00:09:36.613 回答
3

I found another option so you can just use @Html.EditorFor() with templates:

Say I have this enum:

public enum EmailType { Pdf, Html }

I can put this code in Views/Shared/EditorTemplates/EmailType.cshtml

@model EmailType
@{
    var htmlOptions = Model == EmailType.Html ? new { @checked = "checked" } : null;
    var pdfOptions = Model == EmailType.Pdf ? new { @checked = "checked" } : null;
}

@Html.RadioButtonFor(x => x, EmailType.Html, htmlOptions) @EmailType.Html.ToString()
@Html.RadioButtonFor(x => x, EmailType.Pdf, pdfOptions) @EmailType.Pdf.ToString()

Now I can simply use this if I want to use it at any time:

@Html.EditorFor(x => x.EmailType)

It's much more universal this way, and easier to change I feel.

于 2011-06-03T04:02:20.447 回答
2

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")%>
于 2010-10-03T20:23:28.220 回答
2

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.

于 2019-04-01T09:27:33.860 回答
1
<%: Html.RadioButtonFor(m => m.Gender, "Male", new { @checked = true } )%>

or

@checked = checked

if you like

于 2010-10-13T20:10:05.723 回答
0

如果您使用的是 jquery,则可以在单选按钮之前调用它。

$('input:radio:first').attr('checked', true);

^ 这将选中第一个单选框,但您可以查看更多 jquery 以循环到您想要选择的那个。

于 2010-05-20T19:57:36.323 回答
0
           @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
于 2011-11-05T15:03:10.117 回答
0

Here is code to set default radio button set to true

@Html.RadioButtonFor(m => m.Gender, "Male", new { @checked = "checked", id = "rdGender", name = "rbGender" })
于 2015-10-01T10:28:22.340 回答
0

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

Gender = "Male";
于 2016-09-15T15:14:54.667 回答
-1

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

于 2016-10-26T15:28:59.950 回答