IValidatableObject在MVC3 - 客户端验证 [英] IValidatableObject in MVC3 - client side validation

查看:166
本文介绍了IValidatableObject在MVC3 - 客户端验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用MVC3你必须添加自定义验证两个新的机制。这些是:(1)亚类ValidationAttribute或(2)实现IValidatableObject

With MVC3 you have two new mechanisms for adding custom validation. These are (1) subclass ValidationAttribute or (2) implement IValidatableObject.

该ValidationAttribute允许您通过实现IClientValidatable(注册和jQuery通过一个新的适配器和方法)来添加客户端验证相对简单。

The ValidationAttribute allows you to add client side validation relatively simply by implementing IClientValidatable (and registering a new adapter and method via jQuery).

IValidatableObject更适合于一次性的验证要求,其中重用是不是一种选择。这也导致了slighlty简单code。因此,这将是我的一些场景选择。不幸的是,我没有看到使用这种方法实现客户端验证的一个简单的方法。

IValidatableObject is more suited to one-off validation requirements where reuse is not an option. It also results in slighlty simpler code. It would therefore be my choice for a number of scenarios. Unfortunately, I do not see an easy way of implementing client side validation using this method.

所以,问题是什么我失踪,你怎么使用IValidatableObject时,你得到JS验证?

So the question is what am I missing and how DO you get JS validation when using IValidatableObject?

推荐答案

由于我没有得到一个(有效的)答案在这里,我问了几个来自微软的人,他们证实,这是目前不可能。

As I did not get a (valid) answer here, I asked a couple of people from Microsoft and they confirmed that it was not currently possible.

布拉德·威尔逊:

在这个时候,只有属性级的验证器能发出客户端
  验证(作为线与输入的想法好得多
  在浏览器中的表单验证......没有模型说话
  的,从浏览器的点)。

At this time, only property level validators can emit client-side validation (as that lines up much better with the idea of input validation in the form of the browser... there is no "model" to speak of, from the browser's point of view).

司徒韭菜:

我不相信你可以连接客户端验证与
  IValidatableObject

I don't believe you can hook up client validation with IValidatableObject

这篇关于IValidatableObject在MVC3 - 客户端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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