Tuesday 11 November 2014

Benefits of Html Helpers in Asp.Net MVC

So today i am going to give an overview on Html helpers in Asp.Net MVC . So first questions that stuck our mind are -

 What are Html helpers ?
 Why to use them instead of using simple Html coded controls
 What are their benefits in Asp.Net MVC

Html helpers are very much similar to Asp.Net web controls . But Html helpers are much more lightweight as compared to Asp.net Web forms Server controls . The similarity between server controls in Asp.net webforms and Html helpers in Asp.Net MVC are Viewstate . Means they both contain the ability to hold or update the data on postbacks using ViewStates for each controls .

Main  Benefits Of using Html Helpers in Asp.net MVC

  • MVC Html Helpers contains Overloaded Methods like IPostBackDataHandler that Pre-populate the values of html helpers means it works similar to as viewstates
  • Html Helpers is also having one more advantage It provide inbuilt Validation for html helpers textboxes 
Available HTML Helpers 

There following HTML helpers can be used to render (modify and output) HTML form elements:

  1. BeginForm()
  2. EndForm()
  3. TextArea()
  4. TextBox()
  5. CheckBox()
  6. RadioButton()
  7. ListBox()
  8. DropDownList()
  9. Hidden()
  10. Password()

Sample Code To Demonstrate Use of Html Helpers


ASPX CODE

<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm()){%>
<p>
<label for="FullName">FullName</label>
<%= Html.TextBox("FullName") %>
<%= Html.ValidationMessage("FullName", "*") %>
</p>
<p>
<label for="Username">Username</label>
<%= Html.TextBox("Username") %>
<%= Html.ValidationMessage("Username", "*") %>
</p>
<p>
<label for="Password">Password:</label>
<%= Html.Password("Password") %>
<%= Html.ValidationMessage("Password", "*") %>
</p>
<p>
<label for="Password">Confirm Password:</label>
<%= Html.Password("ConfirmPassword") %>
<%= Html.ValidationMessage("ConfirmPassword", "*") %>
</p>
<p>
<%= Html.CheckBox("ReceiveNewsletter") %>
<label for="ReceiveNewsletter" style="display:inline">Receive Newsletter?</label>
</p>
<p>
<input type="submit" value="Register" />
</p>
<%}%>

RAZOR CODE

@Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.")
@using (Html.BeginForm()){ 
<p>
<label for="FullName">FullName</label>
@Html.TextBox("FullName")  
@Html.ValidationMessage("FullName", "*")  
</p>
<p>
<label for="Username">Username</label>
@Html.TextBox("Username")  
@Html.ValidationMessage("Username", "*")  
</p>
<p>
<label for="Password">Password:</label>
@Html.Password("Password")  
@Html.ValidationMessage("Password", "*")  
</p>
<p>
<label for="Password">Confirm Password:</label>
@Html.Password("ConfirmPassword")  
@Html.ValidationMessage("ConfirmPassword", "*")  
</p>
<p>
@Html.CheckBox("ReceiveNewsletter")  
<label for="ReceiveNewsletter" style="display:inline">Receive Newsletter?</label>
</p>
<p>
<input type="submit" value="Register" />
</p>

Share this

1 Response to "Benefits of Html Helpers in Asp.Net MVC"

  1. Really simple and point to point description thanks buddy .........I am a beginner in mvc this helped me too much

    ReplyDelete