In this tutorial, you will learn:
- What is
Html.NameFor
helper method? - How to Use
Html.NameFor
Method in MVC? - Programming Example
Html.NameFor
extension method prints the name of model properties.
Example:
Model: UserModel.cs
namespace HtmlHelperDemo.Models { public class UserModel { public string UserName { get; set; } public int Age { get; set; } public string City { get; set; } } }
View: Index.cshtml
@using HtmlHelperDemo.Models @model UserModel <h1>Html.BeginFor Example</h1> @using (Html.BeginForm("Index", "Home", FormMethod.Post)) { <strong>@Html.NameFor(m => m.UserName)</strong> @Html.TextBoxFor(m => m.UserName)<br /> <strong>@Html.NameFor(m => m.Age)</strong> @Html.TextBoxFor(m => m.Age)<br /> <strong>@Html.NameFor(m => m.City)</strong> @Html.TextBoxFor(m => m.City)<br /> <input id = "Submit" type = "submit" value = "submit" /> }
Output