If my model have
[DisplayName(\"First Name\")]
public string firstName { get; set; }
Then I can print it in the View with LabelFor
@Html.DisplayFor(model => model.acc_first)
should work for you. If not, try just
@Model.acc_first
Either one should work fine.
You can access the value of the attribute Display(Name="...")
in 4 steps:
var type = typeof(YourNamespace.Models.YourModelName);
var memInfo = type.GetMember("firstName"); // your member
var attributes = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), false);
var displayname = ((System.ComponentModel.DataAnnotations.DisplayAttribute)attributes[0]).Name;
Then displayname
will have the value First Name
in your context.
<label asp-for="firstName">First Name</label>
you must change for to asp-for
you should simply use
[Display(Name = "First Name")]
public string name{ get; set; }
Building on Darren Oster's answer:
@Html.DisplayNameFor(x => x.acc_first)
Here's a link to the documentation for this: https://msdn.microsoft.com/en-us/library/system.web.mvc.html.displaynameextensions.displaynamefor(v=vs.118).aspx
You could use this in your view code like this:
@{
var foo = Html.DisplayNameFor(x => x.acc_first);
// call function
DoStuff(foo);
}
@Html.DisplayNameFor(x => x.acc_first)