绑定<;输入类型=“;无线电“>;带有Model字段

本文关键字:带有 gt Model 字段 无线电 lt 输入 类型 绑定 | 更新日期: 2023-09-27 18:30:07

在一个视图中,我有两个<input type="radio">,将它们组合成一个无线电组。

我想将它们与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>

绑定<;输入类型=“;无线电“>;带有Model字段

键入为radio的输入需要设置一个值。如果选中了收音机,则该值就是为模型中的名称发送的值。

视图模型

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

创建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)

手动代码将selected属性添加到两个单选按钮中。以下所有内容都是等效的:

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

请注意,最后两个是checked属性的无效值,但仍要选中单选按钮。由于您有一个组名称,并且只能目视检查一个按钮,因此会选择最后一个渲染的按钮。请参阅W3C规范。

使用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的值为"所有贷款人",则将选择第一个选项。如果值为"主要贷款人",则第二个选项将选择