Question

I am using foreach loop insoide my view to display few radiobutton rows..

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>



Because i was getting problem while Model binding therefore i created manual id to suit my requirement(different incremented id ).
But again problems comes with name attribute i think. for first and every loop i am getting same name attribute(not incremented) i.e if i select radiobuttton from first loop then it deselect taht row from other loop.

Like

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

as you can see name attribute for all loops are same, with different id.
Now my Question is...Is it posssible to override name attribute of RadioButtonFor like id??

Was it helpful?

Solution

Now my Question is...Is it posssible to override name attribute of RadioButtonFor like id??

No, that's not possible. The name attribute will be calculated from the lambda expression you passed as first argument.

Personally I would use editor templates and not bother with any loops at all

@model MainViewModel
<table>
    <thead>
        ...
    </thead>
    <tbody>
        @Html.EditorFor(x => x.main)
    </tbody>
</table>

and in the corresponding editor template:

@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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top