ASP.NET MVC 3验证:是DataAnnotation属性无用的JavaScript被禁用? [英] ASP.NET MVC 3 validation: Are the DataAnnotation attributes useless when JavaScript is disabled?

查看:124
本文介绍了ASP.NET MVC 3验证:是DataAnnotation属性无用的JavaScript被禁用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

斯科特·格思里博客上讲述ASP.NET MVC 2:模型验证的一年多以前,在他的文章中,控制器与被调用ModelState.IsValid法装饰。从那时起,我们就完成了ASP.NET MVC 3,其中包括的变化比较大,以验证

Scott Guthrie blogged about ASP.NET MVC 2: Model Validation little over a year ago and in his post, the controllers were decorated with calls to ModelState.IsValid-method. Since then we've had the ASP.NET MVC 3 which included quite big changes to the validation.

但必须调用ModelState.IsValid仍然保持不变的要求?是否所有的DataAnnotation属性无用的,如果网站访问者已禁用JavaScript和网站开发者忘记检查ModelState.IsValid的价值?

But has the requirement to call the ModelState.IsValid still stayed the same? Are all the DataAnnotation attributes useless if the site visitor has disabled the JavaScript and the site developer has forget to check the value of ModelState.IsValid?

如果是的,是有办法解决?难道例如可以注册一个全球性的过滤器,总是记得检查模型的有效性事件如果coder不?

If yes, is there a way around this? Is it for example possible to register a global filter which always remembers to check for the model's validity event if the coder doesn't?

推荐答案

客户端验证功能将被关闭。这就是为什么你必须从来没有在客户端验证只能依靠。它不会影响其使用的注释服务器上的模型粘合剂。下面是从博客文章中的有关内容...

The client side validation features would be turned off. This is why you must never rely only on client side validation. It would not affect the model binder which uses the annotations on the server. Here is the relevant text from that blog post...

由于操作方法接受
  人对象作为参数,
  ASP.NET MVC会创建一个Person
  对象并自动映射
  进来的表单输入值到它。如
  这个过程的一部分,它也将
  检查是否
  DataAnnotation验证特性
  为Person对象是有效的。如果
  一切是有效的,则
  ModelState.IsValid内检查我们
  code将返回true - 在这种情况下,
  我们将(最终)保存人
  到一个数据库,然后重定向回
  到家庭页。

Because the action method accepts a "Person" object as a parameter, ASP.NET MVC will create a Person object and automatically map the incoming form input values to it. As part of this process, it will also check to see whether the DataAnnotation validation attributes for the Person object are valid. If everything is valid, then the ModelState.IsValid check within our code will return true – in which case we will (eventually) save the Person to a database and then redirect back to the home-page.

这篇关于ASP.NET MVC 3验证:是DataAnnotation属性无用的JavaScript被禁用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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