使用 DataAnnotations 和 ASP.Net MVC 的多个 MetadataType 验证规则集

发布于 2024-10-11 03:44:55 字数 438 浏览 6 评论 0原文

我正在 ASP.Net MVC 中使用 DataAnnotations 和 MetadataType 通过我们 www 网站上的表单来验证我的自定义对象之一的创建。它运作得非常好。

但现在我们还需要能够通过内部管理站点中的表单创建相同的对象。

然而,验证规则略有不同,因为某些字段在 www 网站上是强制性的,而当我们通过内部管理系统自己填写表单时,这些字段不是强制性的。

此外,我希望能够为同一字段提供不同的值DisplayNames 和不同的验证消息取决于从哪个站点/表单收集数据等。

我如何才能本质上拥有两个不同的 MetadataType 并指定在管理站点内验证时与 www 站点相比我希望使用哪一个。即两个不同的验证规则集以及指定要验证的规则集的能力。

我使用了使用 Buddy(部分)类的 MetadataType,因为我的对象是由 LINQ to SQL 自动生成的。

I am using DataAnnotations and MetadataType in ASP.Net MVC to validate the creation of one of my custom objects through a form on our www site. It's working really well.

But now we also need to be able to create the same object through a form in our internal admin site.

However, the validation rules are slightly different in that some fields are mandatory on the www site that are not mandatory when we complete the form ourselves through our internal admin system..

Further, I'd like to be able to give the same field different DisplayNames and different validation messages depending on which site/form the data is being collected from etc.

How can I essentially have two different MetadataType's and specify which one I wish to use when validating within the admin site, versus the www site.. I.e. two different sets of validation rules and the ability to specify which one I am validating against..

I have employed my MetadataType's using Buddy (partial) classes, as my objects are auto-generated by LINQ to SQL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

岁月静好 2024-10-18 03:44:55

我以前也遇到过同样的情况。我当时四处寻找,但发现没有解决方案可以为同一类提供两套验证规则。

我解决这个问题的方法是使用视图模型。您拥有“核心”模型类,并且希望不同的 UI(在本例中为 Web 和管理 UI)具有不同的验证规则。在这种情况下,您的模型类不需要伙伴类,因为您不想在模型类本身上应用验证规则,而是需要从模型类继承来创建两个视图模型类,一个用于 Web另一个用于管理界面,并根据您的需要在这些类上使用不同的 DataAnnotations 应用验证规则。您还可以使用任何额外的、特定于 UI 的属性来“增强”视图模型类。

我知道这个解决方案并不完美,因为您将在两个不同的地方使用验证规则,通常不建议这样做,但它有效,而且实际上并没有那么糟糕,特别是如果应用程序不是很大的话。唯一的其他解决方案是手动检查用户使用的位置来自(网络或管理员),然后根据该情况添加模型状态错误。但我不建议这样做。

我很想听听是否有人对此有更好的解决方案。

I have been in the same situation before. I searched around at the time but found that there was no solution that would give you two sets of validation rules on the same class.

The way I tackled it was to use the view models. You have your "core" model classes and you want different UI's (in this case web and admin UI's) to have different validation rules. You wouldn't need buddy classes for your model classes in this case as you don't want to apply validation rules on the model class itself, instead you will need to inherit from your model class to create two view model classes, one for web and the other for admin interface, and apply the validation rules using DataAnnotations differently on those classes as per your needs. You can also "enhance" your view model classes with any extra, UI specific, attributes.

I know this solution is not perfect as you will have your validation rules on two different places and it's usually not advisable but it works and it's not that bad practically especially if the application isn't very large. The only other solution is to check manually the place user is using is coming from (web or admin) and then adding model state errors according to that. But I wouldn't recommend doing it that way.

I would love to hear if somebody has a better solution for this.

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