且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

所有的单选按钮组合在一起在型号列表中的所有项目

更新时间:2022-12-12 20:11:57

您需要使用循环生成控件集合(不是的foreach 循环,从而产生重复名称 ID 属性,也不会回发到集合)

You need to use a for loop to generate controls in a collection (not a foreach loop which generates duplicate name and ID attributes and would not post back to a collection)

由于每一个单选按钮,您正在生成的同名属性<输入类型=无线电名称=ChosenText... /> 它们都属于同一组只允许一个被选中。

Because every radio button you are generating the same name attribute <input type="radio" name="ChosenText" ... /> they all belong to the same group allowing only one to be selected.

for(int i = 0; i < Model.Count; i++)
{
  @Html.EditorFor(m => m[i].Name)
  <br />
  @Html.EditorFor(m => m[i].City) 
  @foreach (var ans in Model[i].SomeTextList)
  {
    <label>
      @Html.RadioButtonFor(m => m[i].ChosenText, ans)
      <span>@ans</span>
    </label>
  }
}

这将产生(用于单选按钮)

This will generate (for the radio buttons)

<input type="radio" name="[0].ChosenText" value="abc" />
<input type="radio" name="[0].ChosenText" value="xyz" />
.....
<input type="radio" name="[1].ChosenText" value="abc" />
<input type="radio" name="[1].ChosenText" value="xyz" />