在模型或视图模型类中,ASP.NET MVC 2验证应该去哪里? [英] Where should ASP.NET MVC 2 validation go: in the model or viewmodel classes?

查看:83
本文介绍了在模型或视图模型类中,ASP.NET MVC 2验证应该去哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的问题确实是验证应该去哪里?我打算使用MetaData装饰-mvc 2的功能.

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?

推荐答案

在View模型中,验证至少应进行一次,因为这是您作为操作参数收到的,并且包含用户输入.您也可以在模型上进行验证.

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.

这篇关于在模型或视图模型类中,ASP.NET MVC 2验证应该去哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆