Can I specify a label for a field in a model?

I would like to specify a “label” for a specific field in the model and use it wherever in my application.

<div class="editor-field"> <%: Html.TextBoxFor(model => model.surname) %> <%: Html.ValidationMessageFor(model => model.surname) %> </div> 

For this, I would like to add something like:

 <%: Html.LabelFor(model => model.surname) %> 

But labelFor already exists and writes out a "last name". But I want to indicate what it should display, for example, "Your last name."
I am sure it is easy = /

+4
source share
3 answers

Since my project was automatically created, I could not (should not) modify the designer.cs file.
So instead, I created a metadata class as described here

 // Override the designer file and set a display name for each attribute [MetadataType(typeof(Person_Metadata))] public partial class Person { } public class Person_Metadata { [DisplayName("Your surname")] public object surname { get; set; } } 
+1
source

Use the Display or DisplayName attribute for the property in your model.

 [Display(Name = "Your surname")] public string surname { get; set; } 

or

 [DisplayName("Your surname")] public string surname { get; set; } 
+8
source

Yes, you can do this by adding a DisplayAttribute (from the System.ComponentModel.DataAnnotations namespace) to the property: something like this:

 [Display(Name = "Your surname")] public string surname { get; set; } 
+1
source

All Articles