如何将RadioButtonFor()设置为默认选中
<%=Html.RadioButtonFor(m => m.Gender,"Male")%>
(Html.RadioButton)有出路而不是(Html.RadioButtonFor)
有任何想法吗?
This question on StackOverflow处理RadioButtonListFor,答案也解决了你的问题。您可以在RadioButtonListViewModel中设置selected属性。
@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
以下是将默认单选按钮设置为true的代码
@Html.RadioButtonFor(m => m.Gender, "Male", new { @checked = "checked", id = "rdGender", name = "rbGender" })
我发现最好只将默认值放在模型的构造函数方法中。
Gender = "Male";
遇到这个并认为我会指出,对于MVC 5,您需要做的就是在模型上设置值。例如:
模型:
public class ExampleModel
{
public PackingListInputModel()
{
RadioButtonField = "One";
}
public string RadioButtonField { get; set; }
}
查看:
@model ExampleModel
@using (Html.BeginForm)
{
@Html.RadioButtonFor(m => m.RadioButtonField , "One")
@Html.RadioButtonFor(m => m.RadioButtonField , "Two")
}
第一个单选按钮(“One”)的状态将被设置为活动状态,因为该值与模型中设置的值相匹配。
如果radiobutton的值与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>
}
}
有关完整代码,请参阅以下链接:使用默认选中呈现引导程序单选按钮组。 stackoverflow answer link
使用简单的方法:
<%= Html.RadioButtonFor(m => m.Gender, "Male", new { Checked = "checked" })%>
它不是太漂亮,但如果你只需要为整个网站实现很少的单选按钮,这样的东西也可能是一个选项:
<%=Html.RadioButtonFor(m => m.Gender,"Male",Model.Gender=="Male" ? new { @checked = "checked" } : null)%>
我假设你应该有一组单选按钮。事情可能会像
<%=Html.RadioButtonFor(m => m.Gender,"Male")%>
<%=Html.RadioButtonFor(m => m.Gender,"Female")%>
<%=Html.RadioButtonFor(m => m.Gender,"Unknown")%>
您可以从控制器中为m.Gender =“Unknown”(或其他内容)指定默认值。
此Helper计算表达式,如果等于它检查单选按钮的值,并且具有与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)
这种方式更普遍,我觉得更容易改变。
您还可以使用相同的ID添加与单选按钮绑定的标签,然后允许用户单击单选按钮或标签以选择该项目。我在这里使用常量为“男性”,“女性”和“未知”,但显然这些可能是模型中的字符串。
<%: 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,你可以在你的单选按钮之前调用它。
$('input:radio:first').attr('checked', true);
^这将检查第一个单选框,但您可以查看更多jquery以循环到您想要选择的那个。