HTML helpers are an easy way to render the appropriate HTML markup in a view and the ASP.NET MVC framework provides a wide selection of them. An HTML helper method returns an HTML-encoded string of the type
MvcHtmlString. The string can be used to render something as simple as a
CheckBox or as complicated as a complete table of data. In this lesson I show you how to use both the standard HTML helpers and the strongly-typed HTML helpers to render HTML in a web page.
MvcHtmlString is a string that has already been HTML-encoded and therefore should not be encoded again.
This is the HTML used to render a
<input id="text1" type="text" value="My TextBox" />
This is the code, used by a view, to render the same
TextBox using an HTML helper:
<%: Html.TextBox("Text1", Model.Value) %>
In this example, the view is using the
Html.TextBox helper method. The first parameter provides the name and id for the
TextBox and the second parameter provides the value.
The script delimiters <
%> are used to mark the beginning and end of a script. The equal sign within the script delimiters, <
%>, is a shortcut for
Response.Write. If you forget the equal sign, the text will not be displayed because the
Reponse.Write method is not called. The colon within the script delimiters, <
%>, is also a shortcut for
Response.Write. But, in this case the string is automatically HTML-encoded.
HTML helpers are really simple to use. They are ...