当基类位于另一个项目中时,MVC 验证不起作用

发布于 2024-10-07 03:42:01 字数 1046 浏览 3 评论 0原文

我们有大量的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖不如嘢 2024-10-14 03:42:01

好吧,我似乎做错的是在“继承”声明中提供完全限定的路径。如果我导入基类名称空间,然后使用部分限定的路径,那么一切似乎都很好!

即...

<MetadataType(GetType(UserListInfoMetaData))> _
Partial Public Class UserListInfo
    Inherits Bob.Fred.Jim.UserListInfo
End Class

不起作用,但是...

Imports Bob.Fred

Class XYX

    <MetadataType(GetType(UserListInfoMetaData))> _
    Partial Public Class UserListInfo
        Inherits Jim.UserListInfo
    End Class
    .
    .
    .

End Class

起作用!

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...

<MetadataType(GetType(UserListInfoMetaData))> _
Partial Public Class UserListInfo
    Inherits Bob.Fred.Jim.UserListInfo
End Class

Does not work, but...

Imports Bob.Fred

Class XYX

    <MetadataType(GetType(UserListInfoMetaData))> _
    Partial Public Class UserListInfo
        Inherits Jim.UserListInfo
    End Class
    .
    .
    .

End Class

Does!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文