I am generating a lot of views using data annotations and a few custom templates.
public class Container
{
[HiddenInput(DisplayValue = false)]
public string Key { get; set; }
[Display(Name = "Full Name")]
[RequiredForRole("Editor"), StringLength(30)]
public string Name { get; set; }
[Display(Name = "Short Name")]
[RequiredForRole("Editor"), StringLength(10)]
public string ShortName { get; set; }
[Display(Name="Maximum Elements Allowed")]
[RequiredForRole("Admin")]
public int MaxSize { get; set; }
[Display(Name = "Components")]
public IList<Component> Components{ get; set; }
}
In the views, I just use @Html.DisplayForModel(), @Html.EditorForModel, etc.
Certain properties need to be editable by users in some roles but hidden for others. As you can see, I’ve implemented a custom validation attribute RequiredForRole which checks a value exists but only if the current user has a certain role.
I really need a custom Display attribute, but as DisplayAttribute is sealed, this doesn’t seem possible.
I want to avoid having lots of different templates for different kinds of users, or start pushing this logic of who sees what down onto the views. What’s the neatest way to solve this problem?
Something like this, maybe. The (BIG) problem is : how to check the current user’s role…
usage