我在我的视图中使用foreach循环来显示几个radiobutton行..
sample radiobutton
<tr>
<td width="30%">
Integrity
</td>
<td width="17%">@Html.RadioButtonFor(x => x.main.ElementAt(i).nested.integrity, 1, new { id = "main_" + i + "__nested_integrity) Poor
</td>
<td width="18%">@Html.RadioButtonFor(x => x.main.ElementAt(i).nested.integrity, 2, new { id = "main_" + i + "__nested_integrity" }) Satisfactory
</td>
<td width="18%">@Html.RadioButtonFor(x => x.main.ElementAt(i).nested.integrity, 3, new { id = "main_" + i + "__nested_integrity" }) Outstanding
</td>
<td width="16%">@Html.RadioButtonFor(x => x.main.ElementAt(i).nested.integrity, 4, new { id = "main_" + i + "__nested_integrity" }) Off
</td>
</tr>
因为我在模型绑定时遇到问题因此我创建了手动ID以满足我的要求(不同的增量id)。
但我认为,问题还会出现名称属性。
对于第一个和每个循环,我得到相同的名称属性(不递增),即如果我从第一个循环中选择radiobuttton然后从其他循环中取消选择taht行。
喜欢
Loop1 id= "main_0__nested_integrity"
Loop2 id= "main_0__nested_integrity"
Loop1 name= "nested.integrity"
Loop2 name= "nested.integrity"
因为你可以看到所有循环的name属性是相同的,具有不同的id 现在我的问题是......是否可以覆盖RadioButtonFor的名称属性,如id?
答案 0 :(得分:3)
现在我的问题是......是否可以覆盖RadioButtonFor的名称属性,如id?
不,那是不可能的。 name属性将根据您作为第一个参数传递的lambda表达式计算。
就个人而言,我会使用编辑器模板,而不是任何循环
@model MainViewModel
<table>
<thead>
...
</thead>
<tbody>
@Html.EditorFor(x => x.main)
</tbody>
</table>
并在相应的编辑器模板中:
@model ChildViewModel
<tr>
<td width="30%">
Integrity
</td>
<td width="17%">
@Html.RadioButtonFor(x => x.nested.integrity, 1) Poor
</td>
<td width="18%">
@Html.RadioButtonFor(x => x.nested.integrity, 2) Satisfactory
</td>
<td width="18%">
@Html.RadioButtonFor(x => x.nested.integrity, 3) Outstanding
</td>
<td width="16%">
@Html.RadioButtonFor(x => x.nested.integrity, 4) Off
</td>
</tr>