2012-05-23 98 views
3

我使用foreach循环insoide我的视图中显示几行单选按钮..可能更改RadioButtonFor的名称?

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,然后取消选择其他循环的行。

Loop1 id= "main_0__nested_integrity" 
Loop2 id= "main_0__nested_integrity" 
Loop1 name= "nested.integrity" 
Loop2 name= "nested.integrity" 

,你可以看到name属性所有环路都一样,用不同的ID。
现在我的问题是...重写RadioButtonFor的名称属性如ID是否可行?

回答

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> 
+0

是疗法任何替代? – RollerCosta

+0

是的,使用编辑器模板。我已经更新了我的答案以示例。 –

+0

这是为我创建编辑器,但我需要radiobuttons – RollerCosta