【发布时间】:2011-02-17 14:05:53
【问题描述】:
似乎默认的 ASP.NET MVC2 Html 帮助程序在使用这样的代码 (EditorTemplates/UserType.ascx) 时会生成重复的 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。)
【问题讨论】:
标签: html asp.net-mvc asp.net-mvc-2 radio-button html-helper