Using preview 4 of ASP.NET MVC Code like:
<%= Html.CheckBox( "myCheckBox", "Click Here", "True", false ) %>
only outputs:
<input type="checkbox" value="True" name="myCheckBox" />
There is a name
there for the form post back but no id
for javascript or labels :-(
I was hoping that changing it to:
Html.CheckBox( "myCheckBox", "Click Here",
"True", false, new { id="myCheckBox" } )
would work - but instead I get an exception:
System.ArgumentException: An item with the same key has already been added.
As if there was already an id somewhere in a collection somewhere - I'm stumped!
The full exception for anyone interested follows (hey - wouldn't it be nice to attach files in here):
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
-
Apparently this is a bug. Because they are adding it to potential rendering values, they just forgot to include it. I would recommend creating a bug on codeplex, and download the source and modify it for your needs.
From Nick Berardi -
Try this:
<%= Html.CheckBox("myCheckbox", "Click here", "True", false, new {_id ="test" })%>
For any keyword you can use an underscore before the name of the attribute. Instead of class you use _class. Since class is a keyword in C#, and also the name of the attribute in HTML. Now, "id" isn't a keyword in C#, but perhaps it is in another .NET language that they want to support. From what I can tell, it's not a keyword in VB.NET, F#, or Ruby so maybe it is a mistake that they force you to use an underscore with it.
Jeff Martin : where is this decribed in documentation?Lance Fisher : I don't know if it in the documentation. I couldn't find it. I think I just read it on Phil Haacks's blog. Also, I think the prefix was changed to an @ from an underscore, so try "new {@id="test"} if the above doesn't work.From Lance Fisher
0 comments:
Post a Comment