Possible to change name of RadioButtonFor?
Asked Answered
P

1

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

Prevost answered 23/5, 2012 at 8:44 Comment(0)
M
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>
Maladroit answered 23/5, 2012 at 8:46 Comment(9)
Yes, use editor templates. I have updated my answer to show an example.Maladroit
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<ChildViewModel> 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.Maladroit
sorry darin this time i am not getting you , can you please elaborate it. I am having a Main class with ICollection<nested> nested as its Navigation property then again nestedOverNested as nested navigation property holding radiobutton login....now please make it clear for me if possiblePrevost
OK, so if the main property is of type ICollection<nested> then your editor template must be ~/Views/Shared/EditorTemplates/nested.cshtml.Maladroit
one more question.. Is it necessary to include all needed scrip and css file in editor template itself? and small in view scriptsPrevost
No, a good practice is that an editor template contains only markup. Script and CSS should be placed into separate files.Maladroit
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?Prevost
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.Maladroit
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 collectionPrevost

© 2022 - 2024 — McMap. All rights reserved.