简体   繁体   English

在Foreach循环中默认选中设置RadioButtonFor()

[英]Set a RadioButtonFor() checked by default within a Foreach loop

I have a weird behavior using @Html.RadioButtonFor Extension Method. 我使用@Html.RadioButtonFor扩展方法有一个奇怪的行为。 I am using a foreach loop to create a list of RadioButton and By ternary operator. 我正在使用foreach循环来创建RadioButton和By三元运算符列表。 I am trying to set the one who respect the condition to checked but It is always the last who is checked. 我试图设置一个尊重条件的人来检查,但它总是最后一个被检查。 I searched similars question but I am not sure to have found something. 我搜索了类似的问题,但我不确定是否找到了什么。 And I don't want to create/use a custom RadioButtonList . 而且我不想创建/使用自定义的RadioButtonList

Here my code in my View : 在我的视图中我的代码:

@foreach (var item in Model.Entities)
        {
        <div>
                 @Html.RadioButtonFor(m => item.Default, item.EntityId,
                       new { @checked = item.Default ? "checked" : "" })//item.Default is a bool and there is only one set to True 
        </div>
        }

But In my browser it is always the last created which is checked even if item.Default is false . 但在我的浏览器,它总是最后创建了被checked即使item.Defaultfalse So is there something 那有什么东西

If the checked attribute is present (no matter what it's actual value is), the browsers see the button as checked. 如果存在checked属性(无论它的实际值是什么),浏览器会将按钮视为已选中。 This is HTML5 behavior. 这是HTML5行为。 I'm guessing all radiobuttons have the checked attribute defined and the browser will select the last button to be the final selected one. 我猜测所有的radiobuttons都定义了checked属性,浏览器将选择最后一个按钮作为最终选择的按钮。

My solution is to create a custom RadioButtonFor extension method, which accepts a boolean for the checked-state and depending on the value add the checked attribute to the htmlAttributes dictionary. 我的解决方案是创建一个自定义的RadioButtonFor扩展方法,它接受一个检查状态的布尔值,并根据值将checked属性添加到htmlAttributes字典。 Something like so: 像这样的东西:

    public static MvcHtmlString RadioButtonFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, object value, object htmlAttributes, bool checkedState)
    {
        var htmlAttributeDictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

        if (checkedState)
        {
            htmlAttributeDictionary.Add("checked", "checked");
        }

        return htmlHelper.RadioButtonFor(expression, value, htmlAttributeDictionary);
    }

You can then use that method in your view (don't forget to add the namespace where you created the extension method and put it in the config) like so: 然后,您可以在视图中使用该方法(不要忘记添加创建扩展方法的命名空间并将其放在配置中),如下所示:

    @foreach (var item in Model.Entities)
    {
    <div>
             @Html.RadioButtonFor(m => item.Default, item.EntityId, new { /* other attributes go here */ }, item.Default)
    </div>
    }

The precedence of operators is causing the problem here; 运营商的优先权在这里造成了问题; as equality comes before comparison, the phrase will be evaluated as 由于在比较之前存在平等,因此该短语将被评估为

(@checked = item.Default) ? "checked" : ""

whereas you want it to be 而你想要它

@checked = (item.Default ? "checked" : "")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM