4

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??

RollerCosta
  • 5,020
  • 9
  • 53
  • 71

1 Answers1

4

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>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Yes, use editor templates. I have updated my answer to show an example. – Darin Dimitrov May 23 '12 at 08:51
  • That's what happen in the custom editor template that I showed in my answer. Make sure that you have respected the naming convention for your editor template. For example if the `main` property on your main view model is of type `IEnumerable` then the editor template file must be located in `~/Views/Shared/EditorTemplates/ChildViewModel.cshtml`. Notice the location and the name of the template. If you don't respect the convention the default template will be used which obviously doesn't generate radio buttons. – Darin Dimitrov May 23 '12 at 10:58
  • sorry darin this time i am not getting you , can you please elaborate it. I am having a Main class with ICollection nested as its Navigation property then again nestedOverNested as nested navigation property holding radiobutton login....now please make it clear for me if possible – RollerCosta May 23 '12 at 11:09
  • OK, so if the `main` property is of type `ICollection` then your editor template must be `~/Views/Shared/EditorTemplates/nested.cshtml`. – Darin Dimitrov May 23 '12 at 11:10
  • one more question.. Is it necessary to include all needed scrip and css file in editor template itself? and small in view scripts – RollerCosta May 23 '12 at 12:06
  • No, a good practice is that an editor template contains only markup. Script and CSS should be placed into separate files. – Darin Dimitrov May 23 '12 at 12:10
  • ok Darin but how can i manage my loop element i.e i want to give functionality to save single loop data.....should i use form inside custom template? – RollerCosta May 23 '12 at 12:27
  • What loop? You don't need any loop. The editor template will automatically be executed for each element of the main collection so that you don't have to write any loops. – Darin Dimitrov May 23 '12 at 12:31
  • ya right i know that (used term loop for better understanding only) ....Now my question is...I want to post one collection at a time (to save it separately ). i.e different save buttons for different collection – RollerCosta May 23 '12 at 12:33