Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"})  
like image 992
Sham Avatar asked Jul 25 '12 13:07

Sham


People also ask

How to change the Label name in ASP net MVC?

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

What is HTML Labelfor?

Html.Label gives you a label for an input whose name matches the specified input text (more specifically, for the model property matching the string expression): // Model public string Test { get; set; } // View @Html. Label("Test") // Output <label for="Test">Test</label>

What is @model in razor?

The @ symbol is used in Razor initiate code, and tell the compiler where to start interpreting code, instead of just return the contents of the file as text. Using a single character for this separation, results in cleaner, compact code which is easier to read.

What is Razor syntax in MVC?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine.


1 Answers

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.

like image 122
Darin Dimitrov Avatar answered Sep 28 '22 04:09

Darin Dimitrov