Overview Of Standard HTML Helpers In MVC

Introduction

This article will explain standard HTML Helpers in MVC. Standard HTML helpers render HTML controls like Anchor, Label, Hidden Input, TextBox, TextArea, DrpdownList, RadioButton, CheckBox, ListBox, Password Input, and HTML Form etc. We can use as per our requirement.

List of standard HTML Helpers

  1. @Html.BeginForm
  2. @Html.ActionLink
  3. @Html.Label
  4. @Html.Hidden
  5. @Html.TextBox
  6. @Html.TextArea
  7. @Html.Password
  8. @Html.RadioButton
  9. @Html.CheckBox
  10. @Html.ListBox
  11. @Html.DropdownList

Step 1

Open Visual Studio 2015, click on New Project, and create an empty web application project.

Screenshot for creating new project-1

Standard HTML Helpers in MVC

After clicking on New Project one window will appear. Select Web from left panel and choose ASP.NET Web Application to give a meaningful name to your project then click on OK as shown in the below screenshot.

Screenshot for creating new project-2

Standard HTML Helpers in MVC

After clicking on OK, one more window will appear. Choose Empty, check on MVC checkbox and click on OK as shown in the below screenshot.

Screenshot for creating new project-3

Standard HTML Helpers in MVC

After clicking on OK, a project will be created with the name of StandardHTML_Hekper_Demo.

Step 2

Right-click on the Controllers folder, select Add then choose Controller as shown in the below screenshot.

Screenshot for adding controller

 Standard HTML Helpers in MVC

After clicking on the controller, a window will appear to choose MVC5 Controller-Empty -- click on Add.

Standard HTML Helpers in MVC

After clicking on Add, another window will appear with DefaultController. Change the name to HomeController then click on Add.

HomeController will be added under the Controllers folder as shown in the below screenshot.
 
Standard HTML Helpers in MVC

Step 3

Right-click on the index action method in controller, and the  Add view window will appear with default index name check (use a Layout page), and click on Add as shown in the below screenshot. The view will be added in views folder under Home folder with name index.

Screenshot for adding view

Standard HTML Helpers in MVC

@Html.BeginForm

This HTML helper creates and renders HTML form element. Action name is first parameter, controller name is second parameter name and form method.
  1. @using (Html.BeginForm("Index""Home", FormMethod.Post))  
  2. {  
  3. }  

@Html.ActionLink

This HTML helper is used to create an anchor tag or anchor link. It renders anchor. Provide link Text to display and Action Name.
  1. @Html.ActionLink ("Home""Index")  

Output

Standard HTML Helpers in MVC

@Html.Label 

The label helper method responsible for creating HTML label is on the browser. It has two parameters -- first label id and label value.
  1. @Html.Label("firstName""First Name")  
Output
Standard HTML Helpers in MVC

@Html.Hidden

Hidden helper is used for hidden HTML input like employee id.
  1. @Html.Hidden("EmployeeId""")  

@Html.TextBox

TheTextBox Helper method renders a textbox in View that has a specified name. We can also add attributes like class, placeholder etc. with the help of overloaded method in which we have to pass objects of HTML Attributes.
  1. @Html.TextBox("txtFirstName"""new { @class = "form-control", placeholder = "First Name" })  
Output
Standard HTML Helpers in MVC

@Html.TextArea

The TextArea Method renders <textarea> element on browser.
  1. @Html.TextArea("address"new { @class = "form-control", rows = "5" })  
Output
 
Standard HTML Helpers in MVC

@Html.Password

The password HTML helper method is responsible for creating password input field on browser.
  1. @Html.Password("password"" "new { @class = "form-control" })  
Output
Standard HTML Helpers in MVC 

@Html.RadioButton

RadioButton helper method is responsible for creating a radio button on the browser. It is rendered as HTML radio button. RadioButton Helper method takes three parameters; i.e. name of the control, the value, and the Boolean value for selecting the value initially.
  1. <div class="btn-group" data-toggle="buttons">  
  2.       <label class="btn btn-primary active">  
  3.           @Html.RadioButton("MaritalStatus""Married"truenew { id = "IsMarried" }) Married  
  4.       </label>  
  5.       <label class="btn btn-primary">  
  6.           @Html.RadioButton("MaritalStatus""Unmarried"falsenew { id = "IsUnmarried" }) Unmarried  
  7.       </label>  
  8.   </div>  
Output
 
Standard HTML Helpers in MVC 

@Html.CheckBox

The CheckBox helper method renders a checkbox and has the name and id that you specify.
  1. <div class="btn-group" data-toggle="buttons">  
  2.         <label class="btn btn-primary">  
  3.             <span class="glyphicon glyphicon-unchecked"></span>  
  4.             @Html.CheckBox("htmlSkill"true) HTML 5  
  5.         </label>  
  6.         <label class="btn btn-primary active">  
  7.             <span class="glyphicon glyphicon-ok-circle"></span>  
  8.             @Html.CheckBox("cssSkill") CSS3  
  9.         </label>  
  10.         <label class="btn btn-primary">  
  11.             <span class="glyphicon glyphicon-unchecked"></span>  
  12.             @Html.CheckBox("bootstrapSkill"false) Bootstrap 4  
  13.         </label>  
  14.     </div>  
Output
Standard HTML Helpers in MVC 

Html.ListBox

The ListBox helper method creates html ListBox with scrollbar on browser. It allows you to choose multiple values by holding ctrl.
  1. @Html.ListBox("Skills",new List<SelectListItem> {  
  2.     new SelectListItem { Text="ASP.NET",Value="1"},  
  3.     new SelectListItem { Text="MVC",Value="2"},  
  4.     new SelectListItem { Text="SQL Server",Value="3"},  
  5.     new SelectListItem { Text="Angular",Value="4"},  
  6.     new SelectListItem { Text="Web API",Value="5"}  
  7. },new { @class="form-control"})  

Output

Standard HTML Helpers in MVC 

@Html.DropdownList

The DropDownList helper renders a drop down list.
  1. @Html.DropDownList("Gender"new List<SelectListItem> {  
  2.                     new SelectListItem {Text="Select Gender",Value="-1" },  
  3.                     new SelectListItem {Text="Male",Value="1" },  
  4.                     new SelectListItem {Text="Female", Value="2" }  
  5.                     }, new { @class = "custom-select" })  

Output

Standard HTML Helpers in MVC