MVC2 DataAnnotations具有继承验证 [英] MVC2 DataAnnotations validation with inheritance
问题描述
我有一个.NET 2.0类的标记virtual.I的属性需要使用类作为一个MVC2应用程序的模型。所以,我创建了一个.NET 3.5类从.NET 2.0类继承并添加了DataAnnotations属性的新类被覆盖的属性。什么我已经做了片断如下
I have a .NET 2.0 class the properties of which are marked virtual.I need to use the class as a model in a MVC2 application. So, I have created a .NET 3.5 class inheriting from the .NET 2.0 class and added the DataAnnotations attributes to the overriden properties in the new class. A snippet of what I have done is below
// .NET 2.0 class
public class Customer
{
private string _firstName = "";
public virtual string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
}
// .NET 3.5 class
public class MVCCustomer : Customer
{
[Required(ErrorMessage="Firstname is required")]
public override string FirstName
{
get { return base.FirstName; }
set { base.FirstName = value; }
}
}
我已经使用了类作为模型使用HtmlFor佣工MVC2视图。 Serverside集团的验证工作正常,但在客户端验证没有。具体地,不显示在页面上的验证错误。
I have used the class as the model for a MVC2 view using the HtmlFor helpers. Serverside validation works correctly but the client side validation does not. Specifically the validation error is not displayed on the page.
我在想什么,或者仅是有可能做到这一点使用哥们类。
What am I missing, or is it only possible to do this using buddy classes.
感谢。
编辑1:
现在我与好友验证类尝试这样做,那也不行。
EDIT 1: I have now tried this with buddy validation classes and that doesn't work either.
编辑2:
我现在已经摸索出供给HtmlFor佣工拉姆达前pression引起的问题。对于例如。
EDIT 2: I have now worked out that the lambda expression supplied to the HtmlFor helpers is causing the problem. For e.g.
Html.TextBoxFor(M => m.FirstName)调用它计算MemberEx pression的DeclaringType(如pression.Body)作为Customer类,而不是ModelMetadata.FromLambdaEx pression方法在MVCCustomer类。
Html.TextBoxFor(m => m.FirstName) calls the ModelMetadata.FromLambdaExpression method which evaluates the DeclaringType of the MemberExpression (expression.Body) as the Customer class and not the MVCCustomer class.
我试图改变在lambda前pression到Html.TextBoxFor((MVCCustomer M)=> m.FirstName),但DeclaringType还是客户。
I have tried changing the lambda expression to Html.TextBoxFor((MVCCustomer m) => m.FirstName) but the DeclaringType is still Customer.
有没有一种方法,我可以得到DeclaringType为类型MVCCustomer,而不是客户。
Is there a way I can get the DeclaringType to be of type MVCCustomer and not Customer.
推荐答案
现在我已经通过使用属性的新关键字在.NET 3.5类,如下
I have now got around this by using the new keyword on the properties in the .net 3.5 class as below
// .NET 2.0 class
public class Customer {
private string _firstName = "";
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}
}
// .NET 3.5 class
public class MVCCustomer : Customer {
[Required(ErrorMessage="Firstname is required")]
public new string FirstName
{
get { return base.FirstName; }
set { base.FirstName = value; }
}
}
这现在可以正常工作和DataAnnotations从MVCCustimer类属性的正确应用。
This now works as expected and the DataAnnotations attributes from the MVCCustimer class are applied correctly.
希望这有助于
这篇关于MVC2 DataAnnotations具有继承验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!