3

ビュー内でforeachループを使用して、いくつかのラジオボタン行を表示しています。

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)。
しかし、やはり問題は名前属性にあると思います。最初とすべてのループで、同じ名前属性(インクリメントされない)を取得しています。つまり、最初のループからradiobuttonを選択すると、他のループから行の選択が解除されます。

好き

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

ご覧のとおり、すべてのループのname属性は同じで、IDが異なります。
今私の質問は...RadioButtonForのname属性をidのようにオーバーライドすることは可能ですか?

4

1 に答える 1

3

今私の質問は...RadioButtonForのname属性をidのようにオーバーライドすることは可能ですか?

いいえ、それは不可能です。name属性は、最初の引数として渡したラムダ式から計算されます。

個人的にはエディターテンプレートを使用し、ループをまったく気にしません。

@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>
于 2012-05-23T08:46:13.837 に答える