Saturday, 4 April 2015

MVC Html Helpers



  1. In Mvc html helpers are much like ASP.NET web form controls.
  2. Html helpers are used to modify HTML But these helpers are very light weight.
  3. Html helpers does not have a view state and a event modal but we have in web form controls.
  4. Html helper basically a method that returns a string.
  5. In MVC we can the inbuild html helpers or we can create our own helpers.

Standard HTML Helpers

  1. HTML Links
  •  The standard way to use the HTML link in MVC is by using the HTML.ActionLink() helper. 
  • With MVC Action Link create a link to the controller Action. 
  For example :
@Html.ActionLink("About this Project", "About")

HTML output
<a href="/Home/About">About this Project</a>

Also we can pass the parameter value to the action
@Html.ActionLink("Delete Record", "Delete", new {Id=2})

HTML Output
<a href="/Home/Delete/3">Delete Record</a>

HTML Form Elements

The following html heplers are used to render(modify and output) html form elements : 

  • BeginForm()
  • EndForm()
  • TextArea()
  • TextBox()
  • CheckBox()
  • RadioButton()
  • ListBox()
  • DropDownList()
  • Hidden()
  • Password()
ASP.NET Syntax C#:

<%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>
<% using (Html.BeginForm()){%>

<label for="FirstName">First Name:</label>
<%= Html.TextBox("FirstName") %>
<%= Html.ValidationMessage("FirstName", "*") %>

<label for="LastName">Last Name:</label>
<%= Html.TextBox("LastName") %>
<%= Html.ValidationMessage("LastName", "*") %>

<label for="Password">Password:</label>
<%= Html.Password("Password") %>
<%= Html.ValidationMessage("Password", "*") %>

<label for="Password">Confirm Password:</label>
<%= Html.Password("ConfirmPassword") %>
<%= Html.ValidationMessage("ConfirmPassword", "*") %>

<label for="Profile">Profile:</label>
<%= Html.TextArea("Profile", new {cols=60, rows=10})%>

<%= Html.CheckBox("Receiveletter") %>
<label for="Receiveletter" style="display:inline">Receive letter?</label>

<input type="submit" value="Register" />

<%}%>



 



No comments:

Post a Comment