ASP.NET MVC - HTML Helpers
HTML Helpers are used to modify HTML output.
HTML Helpers
In MVC, HTML Helpers are similar to traditional ASP.NET Web Form controls.
Like Web Form controls in ASP.NET, HTML Helpers are used to modify HTML. However, HTML Helpers are more lightweight. Unlike Web Form controls, HTML Helpers do not have an event model and view state.
In most cases, an HTML Helper is simply a method that returns a string.
With MVC, you can create your own helpers or use the built-in HTML Helpers.
Standard HTML Helpers
MVC includes standard helpers for most common HTML element types, such as HTML links and HTML form elements.
HTML Links
The simplest way to render an HTML link is to use the HTML.ActionLink() Helper.
In MVC, Html.ActionLink() does not link to a view. It creates a link to a controller action.
Razor Syntax:
ASP Syntax:
The first parameter is the link text, and the second parameter is the name of the controller action.
The above Html.ActionLink() Helper outputs the following HTML:
Some properties of the Html.ActionLink() Helper:
Property | Description |
---|---|
.linkText | URL text (label), the inner text of the anchor element. |
.actionName | The name of the action. |
.routeValues | The values passed to the action, an object containing the route parameters. |
.controllerName | The name of the controller. |
.htmlAttributes | The attribute settings for the URL, an object containing the HTML attributes to set for the element. |
.protocol | The URL protocol, such as "http" or "https". |
.hostname | The hostname of the URL. |
.fragment | The URL fragment name (anchor name). |
Note: You can pass values to the controller action. For example, you can pass the id of a database record to the database Edit action:
Razor Syntax C#:
Razor Syntax VB:
The above Html.ActionLink() Helper outputs the following HTML:
HTML Form Elements
The following HTML Helpers can be used to render (modify and output) HTML form elements:
BeginForm()
EndForm()
TextArea()
TextBox()
CheckBox()
RadioButton()
ListBox()
DropDownList()
Hidden()
Password()
ASP.NET Syntax C#: