当使用像这样的代码 (EditorTemplates/UserType.ascx) 时,默认的 ASP.NET MVC2 Html 助手似乎生成重复的 HTML ID:
<%@ 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) %>
它生成的 HTML 是:
<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" />
这清楚地表明了一个问题。所以我一定是滥用了 Helper 之类的东西。
我可以手动将 id
指定为 html 属性,但我不能保证它是唯一的。
所以问题是如何确保 RadioButton 助手生成的 ID 对于每个值都是唯一的,并且仍然保留生成这些 ID 的约定(因此嵌套模型受到尊重?(最好不要手动生成 ID。)
最佳答案
除了PanJanek的回答:
如果你真的不需要元素有一个 id,你也可以在 htmlAttributes(即 new { id=""}
)参数中指定 id=""
helper 。这将导致在生成的 html 中完全忽略 id 属性。
关于html - 单选按钮生成重复的 HTML id-s,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2801401/