简体   繁体   English

嵌套循环-@RadioButtonFor无法按预期工作

[英]Nested for loops - @RadioButtonFor not working as expected

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)

    }
}

Realizations: 实现:

I've noticed after this answer that my code is creating a single radiobutton for each phone, but isn't tying them together based on homes. 这个答案之后 ,我注意到我的代码正在为每个电话创建一个单选按钮,但并没有根据房屋将它们捆绑在一起。

What am i missing? 我想念什么?

They either both check, or un-check but their not (bound?) also, i can check both of them or un-check both of them ) correctly by the name of the control?? 它们要么都选中,要么取消选中,但是它们没有(绑定?) ,我也可以同时选中它们,也可以取消选中它们两者 )。

在此处输入图片说明

I am not sure it will be possible using RadioButtonFor here. 我不确定是否可以在此处使用RadioButtonFor RadioButtonFor will construct the Name of the radiobutton based on the lambda expression you provide. RadioButtonFor将根据您提供的lambda表达式构造RadioButtonFor按钮的Name If your model were to have a property PrimaryPhone then you could use RadioButtonFor(m=> m.Homes[h].PrimaryPhone) but the way it is here, the lambda expression will cause it to construct a name specific to each phone. 如果您的模型具有属性PrimaryPhone则可以使用RadioButtonFor(m=> m.Homes[h].PrimaryPhone)但lambda表达式将使它构造每个电话的特定名称。

See: How can I override the name attribute of a RadioButtonFor? 请参阅: 如何覆盖RadioButtonFor的name属性?

You can either use the first syntax you suggested to manually construct a radio grouping, or you could consider adding a property to your model to store the primary phone. 您可以使用建议的第一种语法来手动构建无线电分组,也可以考虑在模型中添加一个属性来存储主电话。

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

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