ASP.NET MVC2 Radio Button generates duplicate HTML id-s

Posted by Dmitriy Nagirnyak on Stack Overflow See other posts from Stack Overflow or by Dmitriy Nagirnyak
Published on 2010-05-10T08:54:06Z Indexed on 2010/05/11 7:54 UTC
Read the original article Hit count: 1234

Hi,

It seems that the default ASP.NET MVC2 Html helper generates duplicate HTML IDs when using code like this (EditorTemplates/UserType.ascx):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<UserType>" %>

<%: Html.RadioButton("", UserType.Primary, Model == UserType.Primary) %>
<%: Html.RadioButton("", UserType.Standard, Model == UserType.Standard) %>
<%: Html.RadioButton("", UserType.ReadOnly, Model == UserType.ReadOnly) %>

The HTML it produces is:

<input checked="checked" id="UserType" name="UserType" type="radio" value="Primary" /> 
<input id="UserType" name="UserType" type="radio" value="Standard" /> 
<input id="UserType" name="UserType" type="radio" value="ReadOnly" /> 

That clearly shows a problem. So I must be misusing the Helper or something.

I can manually specify the id as html attribute but then I cannot guarantee it will be unique.

So the question is how to make sure that the IDs generated by RadioButton helper are unique for each value and still preserve the conventions for generating those IDs (so nested models are respected? (Preferably not generating IDs manually.)

Thanks,
Dmitriy,

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about asp.net-mvc-2