How to change the display name for LabelFor in razor in mvc3?
Asked Answered
M

5

90

In razor engine I have used LabelFor helper method to display the name

But the display name is seems to be not good to display. so i need to change my display name how to do it....

@Html.LabelFor(model => model.SomekingStatus, new { @class = "control-label"}) 
Moye answered 25/7, 2012 at 13:49 Comment(1)
This is a very old question, but still very relevant to current MVC coding. There are problems with the two solutions provided so far, so I have added a more detailed answer covering all 3 options available to you.Version
B
166

You could decorate your view model property with the [DisplayName] attribute and specify the text to be used:

[DisplayName("foo bar")]
public string SomekingStatus { get; set; }

Or use another overload of the LabelFor helper which allows you to specify the text:

@Html.LabelFor(model => model.SomekingStatus, "foo bar")

And, no, you cannot specify a class name in MVC3 as you tried to do, as the LabelFor helper doesn't support that. However, this would work in MVC4 or 5.

Backbend answered 25/7, 2012 at 13:53 Comment(8)
@raj2sekar1, if it is working and this answer helped you solve the problem you were having you should consider marking it as answer by clicking on the tick next to it: meta.stackexchange.com/questions/5234/…Backbend
You need to using System.ComponentModel though.Defelice
This was useful, thank you. But I think your answer would be better if you had this instead @Html.LabelFor(model => model.SomekingStatus, "something other than foo bar")Northrop
Perhaps this written a while ago, but now you can specify class name inside the LabelFor: e.g. @Html.LabelFor(model => model.SomekingStatus, new { @class = "your-css-class" })Oratorical
But if you update the database, the model classes are regenerated and you lose this. How do you avoid that?Maurizio
Note: LabelFor does allow setting the class that way for MVC4/5.Mellow
@shim: Very good question not covered by any existing answers.I have added a detailed answer with all 3 options available to you. The third option will avoid "regeneration" and "duplication of label" problems.Version
It's good to go with Model.Heavy
V
31

This was an old question, but existing answers ignore the serious issue of throwing away any custom attributes when you regenerate the model. I am adding a more detailed answer to cover the current options available.

You have 3 options:

  • Add a [DisplayName("Name goes here")] attribute to the data model class. The downside is that this is thrown away whenever you regenerate the data models.
  • Add a string parameter to your Html.LabelFor. e.g. @Html.LabelFor(model => model.SomekingStatus, "My New Label", new { @class = "control-label"}) Reference: https://msdn.microsoft.com/en-us/library/system.web.mvc.html.labelextensions.labelfor(v=vs.118).aspx The downside to this is that you must repeat the label in every view.
  • Third option. Use a meta-data class attached to the data class (details follow).

Option 3 - Add a Meta-Data Class:

Microsoft allows for decorating properties on an Entity Framework class, without modifying the existing class! This by having meta-data classes that attach to your database classes (effectively a sideways extension of your EF class). This allow attributes to be added to the associated class and not to the class itself so the changes are not lost when you regenerate the data models.

For example, if your data class is MyModel with a SomekingStatus property, you could do it like this:

First declare a partial class of the same name (and using the same namespace), which allows you to add a class attribute without being overridden:

[MetadataType(typeof(MyModelMetaData))]
public partial class MyModel
{
}

All generated data model classes are partial classes, which allow you to add extra properties and methods by simply creating more classes of the same name (this is very handy and I often use it e.g. to provide formatted string versions of other field types in the model).

Step 2: add a metatadata class referenced by your new partial class:

public class MyModelMetaData
{
    // Apply DisplayNameAttribute (or any other attributes)
    [DisplayName("My New Label")]
    public string SomekingStatus;
}

Reference: https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute(v=vs.110).aspx

Notes:

  • From memory, if you start using a metadata class, it may ignore existing attributes on the actual class ([required] etc) so you may need to duplicate those in the Meta-data class.
  • This does not operate by magic and will not just work with any classes. The code that looks for UI decoration attributes is designed to look for a meta-data class first.
Version answered 24/7, 2015 at 10:40 Comment(2)
Is there any reason why this wouldn't work in ASP.NET Core 3.1? [Required] works, but not [DisplayName("my label")]Maxinemaxiskirt
Pay attention to what Visual Studio does if you scaffold your controllers and views. It uses the second of the three options listed here. You can add data annotations all day long and never realize it's being overridden in the .cshtmlNosebleed
S
15

You can change the labels' text by adorning the property with the DisplayName attribute.

[DisplayName("Someking Status")]
public string SomekingStatus { get; set; }

Or, you could write the raw HTML explicitly:

<label for="SomekingStatus" class="control-label">Someking Status</label>
Steerage answered 25/7, 2012 at 13:52 Comment(3)
@shim: You shouldn't really be working directly with database/domain model classes in your views, typically there would be an intermediary 'view-model' class in between.Governorship
exacly what i was looking for using label syntax..Thanks @xanderBrandon
The problem with "write the raw HTML explicitly", is that you will have to write the label yourself everytime you use that form. It would be better to declare de DisplayName on the model.Ancheta
C
4

Decorate the model property with the DisplayName attribute.

Caia answered 25/7, 2012 at 13:53 Comment(0)
C
1

@Html.LabelFor(model => model.SomekingStatus, "foo bar")

Cochineal answered 8/11, 2017 at 12:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.