How to get user id for visualization using HtmlHelper in MVC

Using ASP.NET MVC Preview 4 Code:

<%= Html.CheckBox( "myCheckBox", "Click Here", "True", false ) %> 

outputs only:

 <input type="checkbox" value="True" name="myCheckBox" /> 

There is a name for the post back form, but no id for javascript or labels: - (

I was hoping to change it to:

 Html.CheckBox( "myCheckBox", "Click Here", "True", false, new { id="myCheckBox" } ) 

will work, but instead I get an exception:

 System.ArgumentException: An item with the same key has already been added. 

As if there was already somewhere in the collection somewhere - I'm at a standstill!

A complete exception for any interested person (hey - it would not be a bad idea to attach files 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 
+6
asp.net-mvc html-helper
source share
2 answers

Try the following:

 <%= Html.CheckBox("myCheckbox", "Click here", "True", false, new {_id ="test" })%> 

For any keyword, you can use the underscore in front of the attribute name. Instead of a class, you use _class. Since the class is a keyword in C #, it is also an attribute name in HTML. Now "id" is not a keyword in C #, but it may be in another .NET language that they want to support. From what I can tell, this is not a keyword in VB.NET, F #, or Ruby, so maybe this is a mistake that forces you to use the underscore.

+5
source share

This seems to be a mistake. Since they add it to potential rendering values, they just forgot to include it. I would recommend creating an error on codeplex and downloading the source code and changing it for your needs.

0
source share

All Articles