Asp.net mvc 如何在MVC中使用HtmlHelper获得要渲染的自定义id
使用 代码如下:Asp.net mvc 如何在MVC中使用HtmlHelper获得要渲染的自定义id,asp.net-mvc,html-helper,Asp.net Mvc,Html Helper,使用 代码如下: <%= Html.CheckBox( "myCheckBox", "Click Here", "True", false ) %> 这会起作用,但我得到了一个例外: System.ArgumentException: An item with the same key has already been added. 好像在某个地方的某个集合中已经有了一个id——我被难住了 所有感兴趣的人都有以下例外(嘿,在这里附加文件不是很好吗): System.Argumen
<%= Html.CheckBox( "myCheckBox", "Click Here", "True", false ) %>
这会起作用,但我得到了一个例外:
System.ArgumentException: An item with the same key has already been added.
好像在某个地方的某个集合中已经有了一个id——我被难住了
所有感兴趣的人都有以下例外(嘿,在这里附加文件不是很好吗):
System.ArgumentException:已添加具有相同密钥的项。
在System.ThrowHelper.ThrowArgumentException(异常资源)
at System.Collections.Generic.Dictionary`2.Insert(TKey键、TValue值、布尔加法)
在System.Web.Routing.RouteValueDictionary.Add(字符串键,对象值)中
位于System.Web.Mvc.TagBuilder2.CreateInputTag(HtmlInputType输入类型、字符串名称、RouteValueDictionary属性)
位于System.Web.Mvc.CheckBoxBuilder.CheckBox(字符串htmlName,RouteValueDictionary htmlAttributes)
在System.Web.Mvc.CheckBoxBuilder.CheckBox(字符串htmlName、字符串文本、字符串值、布尔值已检查、RouteValueDictionary htmlAttributes)
位于System.Web.Mvc.CheckBoxExtensions.CheckBox(HtmlHelper助手、字符串htmlName、字符串文本、字符串值、布尔值已检查、对象htmlAttributes)
在c:\dev\myProject\views\account\Edit.ascx中的ASP.views\u account\u terms和conditions\u ascx.\uuuuuu Render\uuuu control1(HtmlTextWriter\uuu w,控制参数容器)中:第108行显然这是一个错误。因为他们正在将它添加到潜在的渲染值中,所以他们只是忘记了包含它。我建议您在codeplex上创建一个bug,然后下载源代码并根据您的需要进行修改。尝试以下方法:
<%= Html.CheckBox("myCheckbox", "Click here", "True", false, new {_id ="test" })%>
对于任何关键字,都可以在属性名称之前使用下划线。你使用的不是类,而是类。因为class是C#中的关键字,也是HTML中属性的名称。现在,“id”在C#中不是一个关键字,但也许他们想支持的是另一种.NET语言。据我所知,它不是VB.NET、F#或Ruby中的关键字,因此,他们强迫您在其中使用下划线可能是一个错误。文档中对此有何描述?我不知道文档中是否有此描述。我找不到它。我想我是在菲尔·哈克斯的博客上读到的。此外,我认为前缀已从下划线改为@,因此如果上述操作无效,请尝试“new{@id=“test”}。
System.ArgumentException: An item with the same key has already been added.
System.ArgumentException: An item with the same key has already been added.
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Web.Routing.RouteValueDictionary.Add(String key, Object value)
at System.Web.Mvc.TagBuilder2.CreateInputTag(HtmlInputType inputType, String name, RouteValueDictionary attributes)
at System.Web.Mvc.CheckBoxBuilder.CheckBox(String htmlName, RouteValueDictionary htmlAttributes)
at System.Web.Mvc.CheckBoxBuilder.CheckBox(String htmlName, String text, String value, Boolean isChecked, RouteValueDictionary htmlAttributes)
at System.Web.Mvc.CheckBoxExtensions.CheckBox(HtmlHelper helper, String htmlName, String text, String value, Boolean isChecked, Object htmlAttributes)
at ASP.views_account_termsandconditions_ascx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\dev\myProject\Views\Account\Edit.ascx:line 108
<%= Html.CheckBox("myCheckbox", "Click here", "True", false, new {_id ="test" })%>