What are HTML helpers in MVC?

Category: C SharpWhat are HTML helpers in MVC?
Editor">Editor Staff asked 2 years ago

What are HTML helpers in MVC?

1 Answers
Editor">Editor Staff answered 2 years ago

With MVC, HTML helpers are much like traditional ASP.NET Web Form controls.
 
Just like web form controls in ASP.NET, HTML helpers are used to modify HTML. But HTML helpers are more lightweight. Unlike Web Form controls, an HTML helper does not have an event model and a view state.
 
In most cases, an HTML helper is just a method that returns a string. 
 
With MVC, you can create your own helpers, or use the built in HTML helpers.
 
Standard HTML Helpers
 
HTML Links
 
The easiest way to render an HTML link in is to use the HTML.ActionLink() helper.With MVC, the Html.ActionLink() does not link to a view. It creates a link to a controller action.
 
ASP Syntax

 

  1. <%=Html.ActionLink(“About this Website”“About”)%>  

The first parameter is the link text, and the second parameter is the name of the controller action.
 
The Html.ActionLink() helper above, outputs the following HTML:

 

  1. <a href=“/Home/About”>About this Website</a>  

The Html.ActionLink() helper has several properties:

  • Property Description.
  • .linkText The link text (label).
  • .actionName The target action.
  • .routeValues The values passed to the action.
  • .controllerName The target controller.
  • .htmlAttributes The set of attributes to the link.
  • .protocol The link protocol.
  • .hostname The host name for the link.
  • .fragment The anchor target for the link.

HTML Form Elements
 
There 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#

 

  1. <%= Html.ValidationSummary(“Create was unsuccessful. Please correct the errors and try again.”) %>  
  2.     <% using (Html.BeginForm()){%>  
  3.         <p>  
  4.             <label for=“FirstName”>First Name:</label>  
  5.             <%= Html.TextBox(“FirstName”) %>  
  6.                 <%= Html.ValidationMessage(“FirstName”“*”) %>  
  7.         </p>  
  8.         <p>  
  9.             <label for=“LastName”>Last Name:</label>  
  10.             <%= Html.TextBox(“LastName”) %>  
  11.                 <%= Html.ValidationMessage(“LastName”“*”) %>  
  12.         </p>  
  13.         <p>  
  14.             <label for=“Password”>Password:</label>  
  15.             <%= Html.Password(“Password”) %>  
  16.                 <%= Html.ValidationMessage(“Password”“*”) %>  
  17.         </p>  
  18.         <p>  
  19.             <label for=“Password”>Confirm Password:</label>  
  20.             <%= Html.Password(“ConfirmPassword”) %>  
  21.                 <%= Html.ValidationMessage(“ConfirmPassword”“*”) %>  
  22.         </p>  
  23.         <p>  
  24.             <label for=“Profile”>Profile:</label>  
  25.             <%= Html.TextArea(“Profile”new {cols=60, rows=10})%>  
  26.         </p>  
  27.         <p>  
  28.             <%= Html.CheckBox(“ReceiveNewsletter”) %>  
  29.                 <label for=“ReceiveNewsletter” style=“display:inline”>Receive Newsletter?</label>  
  30.         </p>  
  31.         <p>  
  32.             <input type=“submit” value=“Register” />  
  33.         </p>  
  34.         <%}%>