当基类位于另一个项目中时,MVC 验证不起作用
我们有大量的 MVC 页面在实体模型和其他本地定义的类上使用验证注释,并且这些页面工作得很好。但是,我现在有一个页面,其基类位于另一个(引用的)项目中,该项目无法正常工作。
某些注释工作正常(例如“DisplayName”),但验证根本不起作用。例如,当提交空白表单时,TryUpdateModel(object) 返回 True,尽管它们在两个字段上都是“必需”验证器。
中的“UserListInfo”类是引用的项目。我尝试过使用和不使用“继承”,但均无济于事。
有什么想法吗?
<MetadataType(GetType(UserListInfoMetaData))> _
Partial Public Class UserListInfo
Inherits [Other Project Namespace].UserListInfo
End Class
Public Class UserListInfoMetaData
<UIHint("HiddenId")> _
Public Property UserID() As Object
<DisplayName("Email Address")> _
<Required(ErrorMessage:="You must enter an Email Address")> _
<StringLength(150, ErrorMessage:="Cannot be more than 150 characters long.")> _
Public Property Username() As Object
<DisplayName("Name")> _
<Required(ErrorMessage:="You must enter a Name")> _
<StringLength(150, ErrorMessage:="Cannot be more than 150 characters long.")> _
Public Property FriendlyName() As Object
End Class
We have a large number of MVC pages using validation annotations on our entity model and other locally defined classes, and these work fine. However I now have a page whose base class is in another (referenced) project which is not working.
Some of the anotations work ok ("DisplayName", for example) but the validation is not working at all. For example, when submitting a blank form, TryUpdateModel(object) returns True, despite their being a "Required" validator on both fields.
The "UserListInfo" class in is the referenced project. I have tried with and without the "Inherits" to no avail.
Any thoughts?
<MetadataType(GetType(UserListInfoMetaData))> _
Partial Public Class UserListInfo
Inherits [Other Project Namespace].UserListInfo
End Class
Public Class UserListInfoMetaData
<UIHint("HiddenId")> _
Public Property UserID() As Object
<DisplayName("Email Address")> _
<Required(ErrorMessage:="You must enter an Email Address")> _
<StringLength(150, ErrorMessage:="Cannot be more than 150 characters long.")> _
Public Property Username() As Object
<DisplayName("Name")> _
<Required(ErrorMessage:="You must enter a Name")> _
<StringLength(150, ErrorMessage:="Cannot be more than 150 characters long.")> _
Public Property FriendlyName() As Object
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我似乎做错的是在“继承”声明中提供完全限定的路径。如果我导入基类名称空间,然后使用部分限定的路径,那么一切似乎都很好!
即...
不起作用,但是...
起作用!
Ok, what I seem to have done wrong was to provide a fully qualified path in the "Inherits" statement. If I import the base class namespace and then use a partially qualified path from that all seems fine!
Ie...
Does not work, but...
Does!