在ASP.NET MVC 3 DRY远程验证 [英] DRY Remote Validation in ASP.NET MVC 3

查看:177
本文介绍了在ASP.NET MVC 3 DRY远程验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过大卫·海登对 MVC 3伟大的职位远程验证

然而,有presented你应该做的,以使远程(JavaScript)的验证。如果用户已禁用JavaScript后仍然会做,即使数据是无效的。因此,一个服务器端验证应该发生。

However there is presented what you should do to enable remote (javascript) validation. If the user has javascript disabled the post would still be made even if data is not valid. Therefore a server-side validation should occur.

我们如何才能让这个检查干(不要重复自己)尽可能?当然,包括在后动作相同的检查code作为远程验证动作(或者一样的调用)可以工作,但我想知道如果一个班轮什么更优雅是可用的。

How could we make this check as DRY (Don't Repeat Yourself) as possible? Of course, including the same check code in the post action as in the remote validation action (or just the same call) can work but I am wondering if a one-liner or something more elegant is available.

完全可以接受的答案,包括不,不能这样做。 :)

Perfectly acceptable answers include "no, it can't be done". :)

推荐答案

这是可以'做..但你需要编写,基本上为散发客户端和验证服务器端自己的自定义属性。对我来说,我只是提取验证code到一个方法,并检查服务器上。
同样的事情也想出了最近还有:

It 'can' be done.. but you would need to write your own custom attribute that basically emits for client side and is validated server side. For me I just extract the validation code into a method and check on the server. Something similar came up recently as well:

<一个href=\"http://stackoverflow.com/questions/5761432/$p$pvent-form-from-submitting-when-using-unbotrusive-validation-in-asp-net-mvc-3/5766366#5766366\">$p$pvent从提交表单的ASP.NET MVC 3 使用unbotrusive验证时

Prevent form from submitting when using unbotrusive validation in ASP.NET MVC 3

我不知道如果一个人想不出从远程属性继承和添加他们自己的服务器端code他们。嗯..也许我得试试这个。

I wonder if one couldnt inherit from the remote attribute and add their own server side code them. hmm.. maybe I'll have to try this.

我会很高兴,虽然如果有人在这里说他们已经这样做:)

I would be happy though if someone here said they already did this : )

这篇关于在ASP.NET MVC 3 DRY远程验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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