【问题标题】:binding <input type="radio"> with Model field?将 <input type="radio"> 与 Model 字段绑定?
【发布时间】:2014-11-11 23:40:46
【问题描述】:

在一个视图中我有两个&lt;input type="radio"&gt;,将它们组合成一个广播组。

我想将它们与 Model 的字段绑定,以某种方式,如果值为 true,则应绑定第一个单选按钮,如果为 false,则应绑定另一个。

请指导如何实现它。我尝试了下面的代码,但无论价值模型有什么,它总是检查第二个收音机。

<div class="radiobuttons">
  <input type="radio" name="LenderType" checked="@Model.Filter_Value" id="rbtnAllLenders" class="cb">
  <input type="radio" id="rbtnMajorLendersOnly" checked="!@Model.Filter_Value" name="LenderType" class="cb">
</div>

【问题讨论】:

  • 向您展示模型(您要绑定的属性是什么以及可能的值是什么)

标签: c# .net asp.net-mvc asp.net-mvc-4 razor-2


【解决方案1】:

类型为 radio 的输入需要设置一个值。如果选中了 radio,那么值就是模型中为 name 发送的值。

查看模型

public class SomeViewModel
{
 public int MyRadioValue { get; set; }
}

输入元素

<input type="radio" value=1 name="MyRadioValue" />
<input type="radio" value=2 name="MyRadioValue" />
<input type="radio" value=3 name="MyRadioValue" />

【讨论】:

    【解决方案2】:

    创建 Html 助手 ....

     public static HtmlString RadioButtonListFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectListItems, RadioButtonListLayout layout)
            {
                var sb = new StringBuilder();
    
                foreach (var item in selectListItems)
                {
                    var itemId = string.Format(
                        CultureInfo.InvariantCulture,
                        "{0}_{1}",
                        helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression)),
                        item.Value);
    
                    var itemHtml = string.Format(
                        CultureInfo.InvariantCulture,
                        "{0} <label for=\"{1}\">{2}</label>",
                        helper.RadioButtonFor(expression, item.Value, new { id = itemId }),
                        itemId,
                        item.Text);
    
                    if (layout == RadioButtonListLayout.Horizontal)
                    {
                        sb.Append("<span class=\"radiobuttonlist-horizontal\">");
                        sb.Append(itemHtml);
                        sb.AppendLine("</span>");
                    }
                    else
                    {
                        sb.Append("<div class=\"radiobuttonlist-vertical\">");
                        sb.Append(itemHtml);
                        sb.AppendLine("</div>");
                    }
                }
    
                return new HtmlString(sb.ToString());
            }
    

    使用它...

    @Html.RadioButtonListFor(x=>x.Id,Model.ListItem,RadioButtonListLayout.Horizontal)
    

    【讨论】:

      【解决方案3】:

      您的手动代码正在将selected 属性添加到两个单选按钮。以下都是等价的:

      <input type="radio" ... checked />
      <input type="radio" ... checked="selected" />
      <input type="radio" ... checked="true" />
      <input type="radio" ... checked="false" />
      

      请注意,最后 2 个是 checked 属性的无效值,但仍检查单选按钮。由于您有一个组名,并且只能直观地检查一个按钮,因此选择了最后一个呈现的按钮。 Refer W3C specifications.

      使用 html 助手绑定到您的模型。如果模型包含属性string LenderType 那么

      @Html.RadioButtonFor(m => m.LenderType, "All lenders", new { id = "All"})<label for="All">All lenders</label>
      @Html.RadioButtonFor(m => m.LenderType, "Major lenders", new { id = "Major"})<label for="Major">Major lenders</label>
      

      如果LenderType 的值为“所有贷方”,则将选择第一个选项。如果值为“主要贷方”,则将选择第二个选项

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-09
        • 1970-01-01
        • 2013-10-17
        • 2015-01-06
        • 1970-01-01
        • 2019-07-10
        • 2022-07-01
        • 1970-01-01
        相关资源
        最近更新 更多