如何在ASp.net MVC 2中将RadioButtonFor()设置为默认值

我如何设置RadioButtonFor()为默认检查

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

(Html.RadioButton)有出路,但不适用于(Html.RadioButtonFor)

有任何想法吗?

StackOverflow上的这个问题涉及RadioButtonListFor,答案也解决了你的问题。 您可以在RadioButtonListViewModel中设置选定的属性。

使用简单的方法:

 <%= Html.RadioButtonFor(m => m.Gender, "Male", new { Checked = "checked" })%> 

我假设你应该有一组单选button。 有可能是这样的

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

你可以给你的控制器m.Gender =“未知”(或其他)的默认值。

这不是太漂亮,但是如果你只需要为整个站点实现非常less的单选button,这样的事情也可能是一个select:

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

这个助手评估expression式,如果等于它检查单选button的值,并且具有与RadioButtonFor相同的参数(因为这个名字是不同的):

 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); } 

用法:

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

结果:

 <!-- 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"> 

我发现另一个选项,所以你可以使用@ Html.EditorFor()与模板:

说我有这个枚举:

 public enum EmailType { Pdf, Html } 

我可以把这个代码放在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() 

现在我可以简单地使用这个,如果我想随时使用它:

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

这种方式更普遍,更容易改变我的感受。

您还可以添加与您的单选button绑定在一起的标签,然后允许用户单击单选button或标签来select该项目。 我在这里使用常量“男”,“女”和“未知”,但显然这些可能是你的模型中的string。

 <%: 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")%> 
 <%: Html.RadioButtonFor(m => m.Gender, "Male", new { @checked = true } )%> 

要么

 @checked = checked 

如果你喜欢

如果你使用jQuery,你可以在你的单选button之前调用它。

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

^这将检查第一个单选框,但你可以看看更多的jquery循环到你想要的select。

  @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 

这是将默认单选button设置为true的代码

 @Html.RadioButtonFor(m => m.Gender, "Male", new { @checked = "checked", id = "rdGender", name = "rbGender" }) 

我发现最好把默认值放在模型的构造函数方法中。

 Gender = "Male"; 

如果单选button的值与Model.Gender值相匹配,则需要在RadioButtonFor中添加“checked”htmlAttribute。

 @{ 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> } } 

有关完整的代码,请参阅下面的链接:使用默认选项呈现引导程序单选button组。 stackoverflow答案链接