You probably don’t want to go back writing the HTML manually, and neither does Microsoft want you to do it. Not only to help you write HTML markup, but also to help you easily bind the data passed from the controller to the view, the ASP.NET MVC Framework comes with a set of helper methods collectively called HtmlHelpers. They are all methods attached to the Html property of the ViewPage. For example, if you want to write the HTML markup for a textbox you just need to write:
<%= Html.Textbox(“propertyName”)%>
And this renders an HTML input text tag, and uses the value of the specified property as the value of the textbox. When looking for the value to write in the textbox, the helper takes into account both the possibilities for sending data to a view: it first looks inside the ViewData hash-table for a key with the name specified, and then looks inside the custom view model, for a property with the given name. This way you don’t have to bother assigning values to input fields, and this can be a big productivity boost, especially if you have big views with many fields.
Let’s see the HtmlHelpers that you can use in your views:
HelperPurpose
Html.ActionLink(text,
actionName, …)
Renders a HTML link with the text specified, pointing to the URL that represents the action and the other optional parameters specified (controller and parameters). If no optional parameters are specified, the link will point to the specified action in the current controller.
Html.RouteLink(text,
routeValues, …)
Renders a HTML link as the method ActionLink, but now using the route values, and optionally the route name, as input.Html.BeginForm(actionName,…)Renders the beginning HTML form tag, setting as action of the form the URL of the action specified. The URL creation works exactly the same as the ActionLink method.Html.EndForm()Renders the form closing tag.Html.Textbox(name)Renders a form input text box, populating it with the value retrieved from the ViewData or custom view model object. Optionally you can specify a different value for the field, or specify additional HTML attributes.Html.TextArea(name, rows, cols, …)Same as Textbox, but renders a textarea, of the specified row and column size.Html.Checkbox(name)Renders a checkbox.Html.RadioButton(name, value)Renders a radio button with the given name, the given value and optionally specifying the checked state.Html.Hidden(name)Renders a form input field of type hidden.
Html.DropDownList(name,
selectList,…)
Renders a select HTML element, reading the options from the selectList variable, which is a list of name-value pairs.
Html.ListBox(name,
selectList,…)
Same as the DropDownList method, but enables the ability to select multiple options.
Html.
ValidationMessage(modelName, …)
Displays a validation message if the specified field contains an error (handled via the ModelState).Html.ValidationSummary(…)Displays the summary with all the validation messages of the view.
Html.
RenderPartial(partialViewName)
Renders on the view the contents of the specified partial view.
As alternative to writing Html.BeginForm and Html.CloseForm methods, you can write an HTML form by including all its elements inside a using block:
<% using(Html.BeginForm(“Save”)) { %>
<!—all form elements here -->
<% } %>
To give you a better idea of how a view that includes an editing form looks like, here is a sample of a complete view for editing an address book element:
<%@ Page Language=”C#” MasterPageFile=”~/Views/Shared/Site.Master”
Inherits=”System.Web.Mvc.ViewPage<EditContactViewModel>” %>
<% using(Html.BeginForm(“Save”)) { %>
Name: <%= Html.Textbox(“Name”) %> <br/>
Surname: <%= Html.Textbox(“Surname”) %> <br/>
Email: <%= Html.Textbox(“Email”) %> <br/>
Note: <%= Html.TextArea(“Notes”, 80, 7, null) %> <br/>
Private <%= Html.Checkbox(“IsPrivate”) %><<br/>
<input type=”submit” value=”Save”>
<% } %>
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}