我使用数据注释和一些自定义模板生成了很多视图。
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; }
}
在视图中,我只使用@Html.DisplayForModel()
,@Html.EditorForModel
等。
某些属性需要由某些角色的用户编辑,但对其他人隐藏。如您所见,我实现了一个自定义验证属性RequiredForRole
,它检查值是否存在,但前提是当前用户具有特定角色。
我真的需要一个自定义的 Display 属性,但由于DisplayAttribute
是密封的,这似乎是不可能的。
我想避免为不同类型的用户提供大量不同的模板,或者开始将这种谁看到什么的逻辑推到视图上。解决这个问题的最简洁的方法是什么?