ASP.NET MVC 2中的MetadataType和客户端验证 [英] MetadataType and client validation in ASP.NET MVC 2

查看:126
本文介绍了ASP.NET MVC 2中的MetadataType和客户端验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继承的属性和MetadataType似乎不适用于ASP.NET MVC 2中的客户端验证.

我们的MetadataTypes的验证可以在服务器上按预期方式工作,但是由于某种原因,它不会为此生成适当的客户端脚本.客户端验证按预期启动了,在PersonView上设置了DataAnnotations属性,因此我知道客户端验证处于活动状态并且可以正常工作. 有人知道是否可以修复它吗?

The validation of our MetadataTypes work as expected on the server but for some reason it does not generate the appropriate client scripts for it. Client side validation kicks in as expected for properties with the DataAnnotations attributes set on the PersonView so I know that client side validation is active and that it works. Does anyone know if or how it can be fixed?

这是我们拥有的:

public abstract class PersonView
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    [Required] public string PhoneNumber { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string AddressZipCode { get; set; }
    public string AddressCity { get; set; }
    public string AddressCountry { get; set; }
}

[MetadataType(typeof(CustomerViewMetaData))]
public class CustomerView : PersonView {}

[MetadataType(typeof(GuestViewMetaData))]
public class GuestView : PersonView {}

public class GuestViewMetaData
{
    [Required(ErrorMessage = "The guests firstname is required")] public string FirstName { get; set; }
    [Required(ErrorMessage = "The guests lastname is required")] public string LastName { get; set; }
}

public class CustomerViewMetaData
{
    [Required(ErrorMessage = "The customers firstname is required")] public string FirstName { get; set; }
    [Required(ErrorMessage = "The customers lastname is required")] public string LastName { get; set; }
    [Required(ErrorMessage = "The customers emails is required")] public string Email { get; set; }
}

如您所见,里面没有什么奇特的东西或奇怪的东西……可以解决吗? 这是ASP.NET MVC 2中的错误吗?

As you can see, it's nothing fancy or strange in there... Can it be fixed? Is it a bug in ASP.NET MVC 2?

推荐答案

根据Microsoft官方,这是ASP.NET MVC 2中的错误.我得到了下面的链接,尽管情况并不完全相同,但这似乎是相同的问题.据我所知,它与继承的属性和DataAnnotations模型元数据提供程序有关.该链接表示,他们将尝试在ASP.NET MVC 3中解决此问题.

According to a Microsoft official this is a bug in ASP.NET MVC 2. I was given the link below and although the scenario isn't exactly the same, it seems to be the same problem. As far as I can tell it is related to inhertited properties and DataAnnotations model metadata provider. The link says they will try to fix the issue in ASP.NET MVC 3.

查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆