在 Foreach 循环中设置默认选中的 RadioButtonFor() [英] Set a RadioButtonFor() checked by default within a Foreach loop

查看:54
本文介绍了在 Foreach 循环中设置默认选中的 RadioButtonFor()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 @Html.RadioButtonFor 扩展方法有一个奇怪的行为.我正在使用 foreach 循环来创建 RadioButton 和 By 三元运算符的列表.我正在尝试将尊重条件的人设置为检查,但始终是最后一个被检查的人.我搜索了类似的问题,但我不确定是否找到了一些东西.而且我不想创建/使用自定义 RadioButtonList.

这是我的视图中的代码:

@foreach(Model.Entities 中的变量项){

@Html.RadioButtonFor(m => item.Default, item.EntityId,新的 { @checked = item.Default ?"checked" : "" })//item.Default 是一个布尔值,只有一个设置为 True</div>}

但在我的浏览器中,即使 item.Defaultfalse,它始终是最后创建的 checked.那么有什么事情吗

解决方案

如果存在 checked 属性(无论它的实际值是什么),浏览器都会将按钮视为已选中.这是 HTML5 行为.我猜所有单选按钮都定义了 checked 属性,浏览器将选择最后一个按钮作为最终选择的按钮.

我的解决方案是创建一个自定义 RadioButtonFor 扩展方法,该方法接受一个布尔值作为选中状态,并根据值将选中属性添加到 htmlAttributes 字典.像这样:

 public static MvcHtmlString RadioButtonFor(this HtmlHelper htmlHelper, Expression> 表达式, 对象值, 对象htmlAttributes, bool checkedState){var htmlAttributeDictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);如果(检查状态){htmlAttributeDictionary.Add("checked", "checked");}返回 htmlHelper.RadioButtonFor(表达式, 值, htmlAttributeDictionary);}

然后您可以在视图中使用该方法(不要忘记添加您创建扩展方法的名称空间并将其放入配置中),如下所示:

 @foreach(Model.Entities 中的变量项){

@Html.RadioButtonFor(m => item.Default, item.EntityId, new {/* 其他属性放在这里 */}, item.Default)</div>}

I have a weird behavior using @Html.RadioButtonFor Extension Method. I am using a foreach loop to create a list of RadioButton and By ternary operator. 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.

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. 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. This is HTML5 behavior. I'm guessing all radiobuttons have the checked attribute defined and the browser will select the last button to be the final selected one.

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. 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>
    }

这篇关于在 Foreach 循环中设置默认选中的 RadioButtonFor()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆