嵌套for循环 - @RadioButtonFor不按预期工作

时间:2015-09-03 15:07:52

标签: c# asp.net-mvc razor

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之后我的代码为每部手机创建了一个单独的按钮,但并没有根据家庭将它们捆绑在一起。

我缺少什么?

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

enter image description here

1 个答案:

答案 0 :(得分:0)

我不确定在这里使用RadioButtonFor是否可行。 RadioButtonFor将根据您提供的lambda表达式构造radiobutton的Name。如果你的模型有一个属性PrimaryPhone,那么你可以使用RadioButtonFor(m=> m.Homes[h].PrimaryPhone)但它的方式,lambda表达式将使它构造一个特定于每个手机的名称。

请参阅:How can I override the name attribute of a RadioButtonFor?

您可以使用建议的第一种语法手动构建广播分组,也可以考虑向模型添加属性以存储主要手机。