DEV Community

Sardar Mudassar Ali Khan
Sardar Mudassar Ali Khan

Posted on

Html helpers in MVC

In ASP.NET MVC, HTML helpers are a set of utility methods provided by the framework to generate HTML markup within views. They help simplify the process of creating HTML elements, handling form controls, rendering URLs, and more. Here are some commonly used HTML helpers in MVC with examples:

  1. TextBoxFor: Generates an HTML input element of type text.
@Html.TextBoxFor(model => model.FirstName)
Enter fullscreen mode Exit fullscreen mode
  1. TextAreaFor: Generates an HTML textarea element.
@Html.TextAreaFor(model => model.Description)
Enter fullscreen mode Exit fullscreen mode
  1. DropDownListFor: Generates a dropdown list for a property.
@Html.DropDownListFor(model => model.CategoryId, Model.Categories)
Enter fullscreen mode Exit fullscreen mode
  1. CheckBoxFor: Generates an HTML checkbox element.
@Html.CheckBoxFor(model => model.IsAdmin)
Enter fullscreen mode Exit fullscreen mode
  1. RadioButtonFor: Generates an HTML radio button element.
@Html.RadioButtonFor(model => model.Gender, "Male")
@Html.RadioButtonFor(model => model.Gender, "Female")
Enter fullscreen mode Exit fullscreen mode
  1. DisplayFor: Displays the value of a property.
@Html.DisplayFor(model => model.LastName)
Enter fullscreen mode Exit fullscreen mode
  1. EditorFor: Generates an editor control for a property.
@Html.EditorFor(model => model.Age)
Enter fullscreen mode Exit fullscreen mode
  1. Partial: Renders a partial view.
@Html.Partial("_PartialViewName", Model)
Enter fullscreen mode Exit fullscreen mode
  1. ActionLink: Generates an HTML anchor link to an action method.
@Html.ActionLink("Home", "Index", "Home")
Enter fullscreen mode Exit fullscreen mode
  1. AntiForgeryToken: Generates a security token to prevent cross-site request forgery (CSRF) attacks.
@Html.AntiForgeryToken()
Enter fullscreen mode Exit fullscreen mode

These are just a few examples of HTML helpers available in ASP.NET MVC. They provide a convenient way to generate HTML markup and handle common scenarios in web development.

Top comments (0)