ASP.NET MVC是/否具有强绑定模型MVC的单选按钮

时间:2010-04-01 08:55:40

标签: c# asp.net-mvc model radio-button boolean

有没有人知道如何将是/否单选按钮绑定到ASP.NET MVC中强类型模型的布尔属性。

模型

public class MyClass
{
     public bool Blah { get; set; }
}

查看

<%@  Page Title="blah"  Inherits="MyClass"%>
    <dd>
        <%= Html.RadioButton("blah", Model.blah) %> Yes
        <%= Html.RadioButton("blah", Model.blah) %> No
    </dd>

由于

解决方案:

感谢Brian的指导,但这与他写的相反。如此 -

<%@  Page Title="blah"  Inherits="MyClass"%>
<dd>
    <%= Html.RadioButton("blah", !Model.blah) %> Yes
    <%= Html.RadioButton("blah", Model.blah) %> No
</dd>

8 个答案:

答案 0 :(得分:194)

如果您正在使用MVC 3和Razor,您还可以使用以下内容:

@Html.RadioButtonFor(model => model.blah, true) Yes
@Html.RadioButtonFor(model => model.blah, false) No

答案 1 :(得分:71)

选择第二个参数,所以使用!当布尔值为假时选择无值。

<%= Html.RadioButton("blah", !Model.blah) %> Yes 
<%= Html.RadioButton("blah", Model.blah) %> No 

答案 2 :(得分:53)

以下是一个更完整的示例,使用fieldset表示可访问性,并将第一个按钮指定为默认值。如果没有fieldset,单选按钮的整体内容无法以编程方式确定。

<强>模型

public class MyModel
{
    public bool IsMarried { get; set; }
}

查看

<fieldset>
    <legend>Married</legend>

    @Html.RadioButtonFor(e => e.IsMarried, true, new { id = "married-true" })
    @Html.Label("married-true", "Yes")

    @Html.RadioButtonFor(e => e.IsMarried, false, new { id = "married-false" })
    @Html.Label("married-false", "No")
</fieldset>

您可以向匿名对象添加@checked参数,以将单选按钮设置为默认值:

new { id = "married-true", @checked = 'checked' }

请注意,您可以使用字符串值替换truefalse来绑定字符串。

答案 3 :(得分:26)

稍微关闭Ben的答案,我添加了ID的属性,以便我可以使用标签。

<%: Html.Label("isBlahYes", "Yes")%><%= Html.RadioButtonFor(model => model.blah, true, new { @id = "isBlahYes" })%>
<%: Html.Label("isBlahNo", "No")%><%= Html.RadioButtonFor(model => model.blah, false, new { @id = "isBlahNo" })%>

我希望这会有所帮助。

答案 4 :(得分:23)

使用常规HTML在单选按钮周围添加标签标签也可以修复'labelfor'问题:

<label><%= Html.RadioButton("blah", !Model.blah) %> Yes</label>
<label><%= Html.RadioButton("blah", Model.blah) %> No</label>

现在点击文本会选择相应的单选按钮。

答案 5 :(得分:8)

或MVC 2.0:

<%= Html.RadioButtonFor(model => model.blah, true) %> Yes
<%= Html.RadioButtonFor(model => model.blah, false) %> No

答案 6 :(得分:5)

如果我可以戴上帽子,我认为有一种比现有答案更清晰的方法来重复使用单选按钮功能。

假设您在 ViewModel 中有以下属性:

Public Class ViewModel
    <Display(Name:="Do you like Cats?")>
    Public Property LikesCats As Boolean
End Class

您可以通过可重复使用的editor template公开该属性:

首先,创建文件Views/Shared/EditorTemplates/YesNoRadio.vbhtml

然后将以下代码添加到 YesNoRadio.vbhtml

@ModelType Boolean?

<fieldset>
    <legend>
        @Html.LabelFor(Function(model) model)
    </legend>

    <label>
        @Html.RadioButtonFor(Function(model) model, True) Yes
    </label>
    <label>
        @Html.RadioButtonFor(Function(model) model, False) No
    </label>
</fieldset>

您可以通过在查看中手动指定模板名称来调用该媒体资源的编辑器:

@Html.EditorFor(Function(model) model.LikesCats, "YesNoRadio")

<强>优点:

  • 在HTML编辑器中编写HTML,而不是在代码后面添加字符串。
  • 保留DisplayName DataAnnotation
  • 允许点击标签以切换单选按钮
  • 维护形式最少的代码(1行)。如果出现错误的方式出现问题,请使用模板进行处理。

答案 7 :(得分:1)

我最终把它打包成扩展方法所以(1)我可以立刻生成标签和广播,(2)所以我不必大惊小怪指定我自己的ID:

public static class HtmlHelperExtensions
{
    public static MvcHtmlString RadioButtonAndLabelFor<TModel, TProperty>(this HtmlHelper<TModel> self, Expression<Func<TModel, TProperty>> expression, bool value, string labelText)
    {
        // Retrieve the qualified model identifier
        string name = ExpressionHelper.GetExpressionText(expression);
        string fullName = self.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);

        // Generate the base ID
        TagBuilder tagBuilder = new TagBuilder("input");
        tagBuilder.GenerateId(fullName);
        string idAttr = tagBuilder.Attributes["id"];

        // Create an ID specific to the boolean direction
        idAttr = String.Format("{0}_{1}", idAttr, value);

        // Create the individual HTML elements, using the generated ID
        MvcHtmlString radioButton = self.RadioButtonFor(expression, value, new { id = idAttr });
        MvcHtmlString label = self.Label(idAttr, labelText);

        return new MvcHtmlString(radioButton.ToHtmlString() + label.ToHtmlString());
    }
}

用法:

@Html.RadioButtonAndLabelFor(m => m.IsMarried, true, "Yes, I am married")