DisplayNameFor :-
It shows the name of the property or the name used in the display attribute of the property.
Example 1:-
[Display(Name = 'Current User')]
public string UserName{get;set;}
@Html.DisplayNameFor(m => m.Username)
So in the browser it will display 'Current User'.
Example 2:-
public string UserName{get;set;}
So for example 2 in the browser it will display 'UserName'.
DisplayFor :-
It is used for displaying model items or database items in the browser.
@foreach (var item in Model)
{
@Html.DisplayFor(modelItem => item.Email)
}
Let us see the above concept Step by Step:-
public ActionResult Index()
{
User objusermodel = new User();
objusermodel.UserName = "USMTECHWORLD";
objusermodel.Email = "usmtechworld@gmail.com";
return View(objusermodel);
}
@model DisplayForDisplayNameFor.Models.User
Usage of DisplayNameFor and DisplayFor :-
@Html.DisplayNameFor(m => m.UserName) @Html.DisplayFor(m=>m.UserName)