I need to have unique Ids for each radio button in a collection in order to attach a jQuery function to only one radio button in the collection. However as noted in this article [link]http://goodcoffeegoodcode.blogspot.com/2010/02/aspnet-mvc-htmlradiobutton-generates.html the MVC radiobuttonfor helper applies the same Id to all buttons of a radio button collection. As the linked article suggests I can override this behavior by applying new {@id = ""}
but this prevents me from assigning a default selection for the radio button collection. Is there a way I can apply a unique Id and a default selection to a MVC radiobuttonfor? If the default MVC radiobuttonfor will not allow me to apply both has someone created a custom MVC helper that does this? Thanks
Asked
Active
Viewed 200 times
0

gemini6609
- 332
- 3
- 7
- 21
-
You can try following: leave id as is and add another html attribute which is unique for each radio button and then in JQuery code query by this attribute. – Yevgeniy.Chernobrivets Aug 12 '14 at 18:11
-
Maybe I should have been more specific, the issue is that is if I specify a default value in a radiobuttonfor tag (such as shown below), there isn't an overload parameter available to apply any html attribute to. `Yes: @Html.RadioButtonFor(x => x.OtherFamilyOwners, "Y", Model.OtherFamilyOwners == "Y") No: @Html.RadioButtonFor(x => x.OtherFamilyOwners, "N", Model.OtherFamilyOwners == "N")` – gemini6609 Aug 12 '14 at 18:51
-
But third overload of RadioButtonFor method accepts dictionary of htmlAttributes? Or am i missing something? http://msdn.microsoft.com/en-us/library/ee830415%28v=vs.118%29.aspx – Yevgeniy.Chernobrivets Aug 12 '14 at 20:13
-
My dumb mistake, I didn't realize that I could specify the selected radio by setting the value to the model property in the controller. Applying the value to the property in the controller, allowed me to use the third htmlAttributes overload to specify a unique id as desired. Thank you Yevgeniy for your help. – gemini6609 Aug 12 '14 at 22:15