简体   繁体   中英

What are the html attributes for html helpers in ASP.NET MVC3 C#?

when you use HtmlHelpers in MVC, you can specify a set of html attributes aa dictionary objects.

I read online a lot of material about creating HtmlHelpers but I am already focusing on jQuery and other ASP.NET stuff for an application that I have to develop ASAP and I do not have time to take care of Html Helpers customization as well.

I know that there are some attributes made already available by MVC3 like @class, but in the Microsoft website there is no information about the htmlattributes. May you please tell me which attributes are available and how I can specify themin C#?

I am mostly interested in the "disable",to disable a specific control, and "id", to change the id of a control, because the HtmlHelpers for default use the same value for id and name. I need it to use them with jQuery.

Thanks in advance

Francesco

Those are simple HTML attributes, so you could use any attribute available in the DOCTYPE you are using. ASP.NET MVC helpers simply take those attributes and emit them in the resulting HTML page. So if you need a documentation take a look at the HTML specification .

There is just a little remark about HTML5 data-* attributes which contain dashes. Because dashes cannot be used as variable names in .NET you could use underscore which will be converted to a dash. Example:

@Html.TextBoxFor(x => x.Foo, new { data_id = "bar" })

which will be rendered as:

<input type="text" name="Foo" id="Foo" value="some value" data-id="bar" />

Html Attributes basically just provides a collection of values to be written as attributes on the corresponding element created

syntax is generally new { @class="#myClass", @id="mySpecificID", @customAttrName="myvalue" }

might generate something like

<input type="text" name="MVCSuppliedName" value="" class="#myClass id="mySpecificID" customAttrName="myvalue" />

They are meant to offer a flexible way to add attributes to rendered tags at runtime.

The Html attributes is simply a collection of key-value pairs that will get rendered in the tag. You can use whatever you want. Consider this code:

Html.ActionLink(
   "My Link", 
   "MyAction", 
   "MyController", 
   new { }, // Route values
   new { disabled = "disabled" } // Html Attributes
);

There are a lot overload for ActionLink , but this one takes an object for routeValues and another object for htmlAttributes .

What you can use in the htmlAttributes collection is limited on what the HTML standard says. But in most browsers you can add attributes of you own if you feel like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM