ASP.NET MVC 2 验证应该去哪里:模型还是视图模型类?

发布于 2024-09-30 07:14:31 字数 123 浏览 1 评论 0原文

我正在使用自动映射器将我的模型映射到视图模型类以传递到我的视图。

我的问题实际上是验证应该去哪里?我计划使用元数据装饰 - mvc 2 的一个功能。

但是是在模型中还是在视图模型中?还是两个地方都有?

I am using automapper to map my models to viewmodel classes to pass to my view.

My question really is where should the validation go? I was planning on using the MetaData decorations - a feature of mvc 2.

But either in the model or viewmodel? Or in both places?

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

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

发布评论

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

评论(5

世界等同你 2024-10-07 07:14:31

验证应该至少在视图模型中完成,因为这是您作为操作参数收到的内容并包含用户输入。您还可以对模型进行验证。

Validation should be done minimum at the View Model because this is what you receive as action argument and contains the user input. You could also have validation at the model.

随心而道 2024-10-07 07:14:31

我的答案是 ViewModel,因为模型可以更改(例如从使用 Linq2SQL 到 EF)。这样,当您插入另一个模型时,您的验证仍然完好无损。

My answer would be ViewModel because Model can change (for example from using Linq2SQL to EF). This way when you plug another Model you still have your validation intact.

回眸一遍 2024-10-07 07:14:31

我个人使用 DataAnnotations 进行了 2 个验证。我的模型没有完全传递到我的视图中。我的视图有单独的模型,并将数据从视图模型转换为模型。这样,我可以将任何我想要的内容放入视图模型中,并忽略我不想处理的部分。

然而,我的理由是我有一个 Windows 应用程序和一个使用相同模型的 Web 应用程序。这样,同一组验证规则可以管理所有应用程序的模型,并且如果需要,我的视图模型可以具有稍微不同的规则。当然,这会产生“逻辑重复”——嗯,验证逻辑。

这样,我就不必每次返回服务器时都重建页面上未使用的数据,或者将其放入隐藏字段并增加页面的大小。

I personally have my validation 2 places using DataAnnotations. My model is not passed up to my view in full. I have separate models for my views and translate the data from the view model into the model. This way, I can put whatever I want in my view model and leave out the pieces I don't want to deal with.

My reasoning, however, is that I have a windows application and an web application using the same model. This way, the same set of validation rules govern the Model for all apps, and my view model can have slightly different rules if need be. Of course, this creates a "duplication of logic" - well, validation logic.

This way I don't have to rebuild the data that wasn't used on the page every trip back to the server or put it in hidden fields and inflate the size of my pages.

初与友歌 2024-10-07 07:14:31

您应该将特定于 UI 的验证放入 ViewModel 中,并将与业务流程或数据库验证相关的任何内容放入模型中。这些可能会重叠。

You should put validation that is specific to the UI in the ViewModel, and anything that is related to business process or database validation in the Model. These might overlap.

内心荒芜 2024-10-07 07:14:31

模型应该实现所需的验证,以确保其状态不会变得无效;该验证绝对属于模型。
例如,书籍类必须保证其标题必须在 1 到 50 个字符之间,其 id 必须 >= 0 等。

业务规则属于其他地方(如果您只有模型视图和控制器层,则在您的控制器中)。这可能类似于如果用户的电子邮件未经过验证,则用户无法添加超过 3 本书。

视图中的验证应仅限于解析用户输入的无效数据:反 xss、sql 注入、超出范围。 ETC

The model should implement the validation it needs to ensure that its state cannot become invalid; that validation most definitely belongs on the model.
for example, a book class must guarantee that its title must be between 1 and 50 characters, its id must be >= 0 etc.

business rules belong elsewhere (in your controllers if you only have the model view and controller layers). this might be something like a user cannot add more than 3 books if their email isnt verified.

validation in the view should be restricted to parsing user input for invalid data: anti xss, sql injection, out of range. etc

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