Different Types of HTML Helpers in ASP.NET MVC
ASP.NET provides a wide range of built-in HTML helpers that can be used as per the user’s choice as there are multiple overrides available for them. There are three types of built-in HTML helpers offered by ASP.NET.
1. Standard HTML Helper
The HTML helpers that are mainly used to render HTML elements like text boxes, checkboxes, Radio Buttons, and Dropdown lists, etc. are called Standard HTML helpers.
List of Standard HTML Helpers
@Html.ActionLink() - Used to create link on html page @Html.TextBox() - Used to create text box @Html.CheckBox() - Used to create check box @Html.RadioButton() - Used to create Radio Button @Html.BeginFrom() - Used to start a form @Html.EndFrom() - Used to end a form @Html.DropDownList() - Used to create drop down list @Html.Hidden() - Used to create hidden fields @Html.label() - Used for creating HTML label is on the browser @Html.TextArea() - The TextArea Method renders textarea element on browser @Html.Password() - This method is responsible for creating password input field on browser @Html.ListBox() - The ListBox helper method creates html ListBox with scrollbar on browser
2. Strongly-Typed HTML Helper
The Strongly-Typed HTML helper takes a lambda as a parameter that tells the helper, which element of the model to be utilized in the typed view. The Strongly typed views are used for rendering specific sorts of model objects, rather than using the overall View-Data structure.
List of strongly-Typed HTML Helper
@Html.HiddenFor() @Html.LabelFor() @Html.TextBoxFor() @Html.RadioButtonFor() @Html.DropDownListFor() @Html.CheckBoxFor() @Html.TextAreaFor() @Html.PasswordFor() @Html.ListBoxFor()
The functionality of all of these are the same as above but they are used with modal classes. Now, as we know we need a model class to use strongly typed HTML. So firstly we will add a model class as follows
Now write the following code in the controller. And then add a view with the default properties.
Now write the HTML as follows:
The output will be as follows:
3. Templated HTML Helper
The templated HTML Helper is used for data display and input. It generates HTML automatically as per model property and it can generate HTML for a complete model with a single tag. These are divided into two categories
- Display Template
- Editor Template
List of Templated HTML Helpers
Display @Html.Display() @Html.DisplayFor() @Html.DisplayName() @Html.DisplayNameFor() @Html.DisplayText() @Html.DisplayTextFor() @Html.DisplayModelFor() Edit / Input @Html.Editor() @Html.EditorFor() @Html.EditorForModel()
Now, here we can use the previously created model class and then we should write this code in the controller
Now add the view with all the default properties and write the following code
The output will be as follows