2012-05-23 30 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