How to change [DisplayName "xxx"] in the controller?

People,

I am new to MVC 2 and stuck with this problem:

AccountModuls.cs

public class LogOnModel { [Required] [DisplayName("User name")] public string UserName { get; set; } … } 

logon.aspx

 <%: Html.LabelFor(m => m.UserName) %> 

The text "Username" will be permanently displayed on the website - based on my definition

[DisplayName ("Username")].

No problems.

But how can I change this text in AccountController.cs?

 public ActionResult LogOn() { return View(); } 
+3
asp.net-mvc-2
source share
1 answer

You cannot :) You need to change the DisplayName attribute in the class so that the .LabelFor helper creates the label. You could, of course, just write the HTML for Label yourself if you want this to be something else.

I don’t understand why you want to change the display name from page to page? Am I a misunderstanding?

Edit:

Custom attribute displayname:

 public class MyDisplayName : DisplayNameAttribute { public int DbId { get; set; } public MyDisplayName(int DbId) { this.DbId = DbId; } public override string DisplayName { get { // Do some db-lookup to retrieve the name return "Some string from DBLookup"; } } } public class TestModel { [MyDisplayName(2)] public string MyTextField { get; set; } } 
+6
source share

All Articles