HTML Helper:-

As we all know that while developing the UI screen we use HTML elements. HTML tags are very simple and easy to use. MVC provides helper for simple HTML elements, let’s see what it is ?

What is HTML helper?

It is collection of methods available in the HTML property of the View.

E.g: @Html.LabelFor(m => m.UserName)

Why we need HTML helper?

  • Instead of directly placing the HTML element inside the view, you can use the HTML helper method to place the tag. While rendering these methods call will be replaced with right html tag.
  • It makes sure elements has proper names and valued for model binding and elements display the appropriate error message when model binding fails.

All razor view inherits the ‘html’ property from its base class. ‘html’ is of type from System.Web.Mvc.HtmlHelper<T>.

Where ‘T’ is the generic type parameter representing the type of model used in view

Automatic Encoding:

HTML helper provides encoding of user input by default, this will helps to avoid cross site scripting attacks (XSS)

@Html.TextArea("text", "hello <br/> world")

Output

            
    <textareacols="20" id="text"
                name="text" 
                rows="2">
    hello<br /> world </textarea>