WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

ASP.NET MVC - HTML Helpers


HTML Helpers are utilized to alter HTML output


HTML Helpers

With MVC, HTML assistants are much similar to customary ASP.NET Web Form controls.

Just like web structure controls in ASP.NET, HTML assistants are utilized to adjust HTML. However, HTML partners are increasingly lightweight. Dissimilar to Web Form controls, a HTML aide does not have an occasion demonstrate and a view state.

In most cases, a HTML aide is only a strategy that profits a string.

With MVC, you can make your very own aides, or utilize the implicit HTML helpers.


Standard HTML Helpers

MVC incorporates standard partners for the most well-known sorts of HTML components, similar to HTML connections and HTML structure elements.


HTML Links

The most effortless approach to render a HTML connect in is to utilize the HTML.ActionLink() assistant.

With MVC, the Html.ActionLink() does not connection to a view. It makes a connection to a controller action.

Razor Syntax:

@Html.ActionLink("About this Website", "About")

ASP Syntax:

<%=Html.ActionLink("About this Website", "About")%>

The first parameter is the connection content, and the second parameter is the name of the controller action.

The Html.ActionLink() aide above, yields the accompanying HTML:

<a href="/Home/About">About this Website</a>

The Html.ActionLink() aide has a few properties:

PropertyDescription
.linkTextThe interface content (label)
.actionNameThe target action
.routeValuesThe values go to the action
.controllerNameThe target controller
.htmlAttributesThe set of ascribes to the link
.protocolThe interface protocol
.hostnameThe have name for the link
.fragmentThe stay focus for the link

Note: You can pass esteems to a controller activity. For model, you can pass the id of a database record to a database alter action:

Razor Syntax C#:

@Html.ActionLink("Edit Record", "Edit", new {Id=3})

Razor Syntax VB:

@Html.ActionLink("Edit Record", "Edit", New With{.Id=3})

The Html.ActionLink() aide above, yields the accompanying HTML:

<a href="/Home/Edit/3">Edit Record</a>

HTML Form Elements

There following HTML aides can be utilized to render (alter and yield) HTML structure 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()){%>
<p>
<label for="FirstName">First Name:</label>
<%= Html.TextBox("FirstName") %>
<%= Html.ValidationMessage("FirstName", "*") %>
</p>
<p>
<label for="LastName">Last Name:</label>
<%= Html.TextBox("LastName") %>
<%= Html.ValidationMessage("LastName", "*") %>
</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>
<label for="Profile">Profile:</label>
<%= Html.TextArea("Profile", new {cols=60, rows=10})%>
</p>
<p>
<%= Html.CheckBox("ReceiveNewsletter") %>
<label for="ReceiveNewsletter" style="display:inline">Receive Newsletter?</label>
</p>
<p>
<input type="submit" value="Register" />
</p>
<%}%>