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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With