Html.NameFor MVC5 Example

In this tutorial, you will learn:
  1. What is Html.NameFor helper method?
  2. How to Use Html.NameFor Method in MVC?
  3. 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 Html.NameFor Example

More Articles

 

Share your thought



PLEASE DISABLE ADBLOCKER AND SUPPORT US!

Please support us by enabling ads on this page. Refresh

YOU DON'T LIKE ADS, WE ALSO DON'T LIKE ADS!
 
But we have to show ads on our site to keep it free and updated. We have to pay huge server costs, domain costs, CDN Costs, Developer Costs, Electricity and Internet Bill. Your little contribution will encourage us to regularly update this site.