I am designing a sort of logging system where each item in the log is accompanied by the user that logged it. The user class uses Data Annotations to modify its display name. When displaying User.Name in one of my log views, I wish to show a different display name.
Classes
public class User
{
public int UserID {set;get;}
[DisplayName("User Name")]
public string Name{set;get;}
}
public class Log
{
public int LogID {set;get;}
public int UserID {set;get;}
public virtual User User{set;get;}
}
If I were to use the following code in my view, the text shown is “User Name”. Is there way to make the DisplayName “Assignee” using Data Annotations, or do I need to this manually?
@Html.DisplayNameFor(model => model.User.Name)
This is the problem when you have view data attached to the model (the display name): Sometimes you need different labels depending the view.
In your case the answer is no, you need to change the label manually.
Or, you will need to create a specific viewModel for your view: