在 C# 中将 MetadataType 添加到派生类
我有一个名为的类
public partial class Contact
{
public int Id { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
我有另一个名为的类
public partial class Person : Contact
{
public string Occupation { get; set; }
public string Country { get; set; }
}
我有我的第三个类名为Person,它被声明为Partial,第四个类名为PersonMetaData,用于声明注释
[MetadataType(typeof(PersonMetadata))]
public partial class Person : Contact
{
}
public class PersonMetadata
{
[StringLength(20, ErrorMessageResourceName = "FirstNameLength",
ErrorMessageResourceType = typeof(BasicErrors))]
[Required(ErrorMessageResourceName = "FirstNameRequired",
ErrorMessageResourceType = typeof(BasicErrors))]
public string FirstName { get; set; }
[StringLength(20, ErrorMessageResourceName = "LastNameLength",
ErrorMessageResourceType = typeof(BasicErrors))]
[Required(ErrorMessageResourceName = "LastNameRequired", ErrorMessageResourceType
= typeof(BasicErrors))]
public string LastName { get; set; }
}
在我的MVC视图中,我制作了一个基于Person的强类型页面?问题是,必需的和字符串长度验证不起作用。当继承类并创建分部类来引用 MetadataType 时,会出现此问题。
如果没有继承,那么 MetadataType 在调用部分类时可以正常工作。
将 MetadataType 用于派生类并与其一起使用部分时有任何解决方案吗?
谢谢
I have a Class called
public partial class Contact
{
public int Id { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
I have another Class called
public partial class Person : Contact
{
public string Occupation { get; set; }
public string Country { get; set; }
}
I have my third class called Person which is declared Partial and Fourth class called PersonMetaData used to declare annotations
[MetadataType(typeof(PersonMetadata))]
public partial class Person : Contact
{
}
public class PersonMetadata
{
[StringLength(20, ErrorMessageResourceName = "FirstNameLength",
ErrorMessageResourceType = typeof(BasicErrors))]
[Required(ErrorMessageResourceName = "FirstNameRequired",
ErrorMessageResourceType = typeof(BasicErrors))]
public string FirstName { get; set; }
[StringLength(20, ErrorMessageResourceName = "LastNameLength",
ErrorMessageResourceType = typeof(BasicErrors))]
[Required(ErrorMessageResourceName = "LastNameRequired", ErrorMessageResourceType
= typeof(BasicErrors))]
public string LastName { get; set; }
}
In my MVC View, I have made a strongly typed page based on Person? The problem is, required and string length validations do not work. This problem occurs when inheriting a class and creating a partial class to reference the MetadataType.
If there is no inheritance then MetadataType works fine when calling a Partial Class.
Any Solutions when using MetadataType for derived class and using partial with it ??
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是MVC2中的问题。然而它在 MVC3 中工作。请参阅以下文章:
It was the problem in MVC2. However it works in MVC3. See the following article :
http://connect.microsoft.com/VisualStudio/feedback/details/538360/asp-net-mvc-2-rc2-the-client-side-validation-does-not-work-with-overridden-properties
删除该部分并尝试以下操作:
Remove that partial and try this: