【发布时间】:2015-06-21 17:26:18
【问题描述】:
我有一个 person 类,就这个问题而言,它只是
public class Person
{
[Required(ErrorMessage = "***")]
[Display(Name = "Your full name")]
public string Name { get; set; }
}
我继承了这是在我的投诉类中
public class Complaints : Person
{
[Required(ErrorMessage = "***")]
[Display(Name = "Detail of the issue")]
public string Detail{ get; set; }
}
一切都按预期进行。
问题是,我们现在希望用户不需要完整的姓名,但我已经将其设置为必需。
由于我的 Person 类在其他地方使用,我无法更改 Required 属性。
如何在派生类中覆盖DataAnnotations?我猜(这也解释了我的困惑)因为DataAnnotation 属于我不能只覆盖DataAnnocation 而必须覆盖整个属性的属性?
【问题讨论】:
-
使用
new关键字。将属性添加到Complaints-public new string Name { get; set; }(不带属性)或更好,使用视图模型。 -
谢谢。在这种情况下如何使用 ViewModel,它与具有 @StephenMuecke 属性的类有何不同?
-
视图模型是特定于视图的类。参考What is ViewModel in MVC?
标签: asp.net-mvc-4