【问题标题】:Nested for loops - @RadioButtonFor not working as expected嵌套 for 循环 - @RadioButtonFor 未按预期工作
【发布时间】:2015-09-03 23:59:33
【问题描述】:
for (var h= 0; h< Model.Homes.Count(); h++)
    for (var p = 0; p< Model.Homes[h].Phones.Count(); p++)

        //this works 
        @Html.RadioButton("Phone"+h.ToString(), h, Model.Homes[index].Phones[p].Phone.IsPrimary.Value)

        //Can't figure this one out
        @Html.RadioButtonFor(m=> m.Homes[index].Phones[p].Phone.IsPrimary, p)

    }
}

实现:

this answer 之后,我注意到我的代码为每部手机创建了一个单选按钮,但并没有根据家庭将它们绑定在一起。

我错过了什么?

他们要么都检查,要么取消检查,但他们没有(绑定?)另外,我可以通过控件的名称正确检查他们两个或取消检查他们两个)? ?

【问题讨论】:

标签: c# asp.net-mvc razor


【解决方案1】:

我不确定是否可以在此处使用RadioButtonForRadioButtonFor 将根据您提供的 lambda 表达式构造单选按钮的 Name。如果您的模型有一个属性 PrimaryPhone,那么您可以使用 RadioButtonFor(m=&gt; m.Homes[h].PrimaryPhone),但在这里,lambda 表达式将导致它为每个电话构造一个特定的名称。

见:How can I override the name attribute of a RadioButtonFor?

您可以使用建议的第一种语法手动构建无线电分组,也可以考虑在模型中添加一个属性来存储主要电话。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 2020-12-27
    • 1970-01-01
    相关资源
    最近更新 更多