向远程验证属性 MVC3 添加延迟
我有一个情况,在执行注册页面时,我必须检查数据库中是否存在用户名。为此,我在模型中实现了用于远程验证的远程属性
[Remote("CheckUserNameAvaliable", "User", Httpmethod="Post")]
public string Username {get; set;}
,我的方法看起来像这样,
[HttpPost]
public JsonResult CheckUserNameAvaliable(string UserName)
{
SessionUser currentUser = this.sessionHelper.GetCurrentUser();
User user = this.userService.GetByUsername(UserName);
if (user != null && user.Id != currentUser.Id)
{
return Json(false);
}
return Json(true);
}
它对我来说效果很好,但问题是,每当我们在用户名文本框上输入密钥时,它都会归档此远程验证,但根据根据我的要求,我必须仅在用户名文本框中输入值后才触发此远程验证。为此,我们可以强制为远程验证属性添加延迟吗?
有谁知道如何在 MVC3 中添加远程验证延迟?
I have a senario where i have to check for a Username existance in database while doing a registration page. for this i have implemented a Remote Attribute for remote validation in my model
[Remote("CheckUserNameAvaliable", "User", Httpmethod="Post")]
public string Username {get; set;}
and my method looks like this
[HttpPost]
public JsonResult CheckUserNameAvaliable(string UserName)
{
SessionUser currentUser = this.sessionHelper.GetCurrentUser();
User user = this.userService.GetByUsername(UserName);
if (user != null && user.Id != currentUser.Id)
{
return Json(false);
}
return Json(true);
}
It works fine for me, but the problem is that whenever we made a key up on the username Textbox it will file this remote validation, but according to my requirement i have to fire this remote validation only after entering value in the username Textbox. For that can we forcefully add a delay to the remote validation attribute?
Do anyone know how to add delay to remote validation in MVC3?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
MVC3在浏览器中使用jquery.validate插件。使用 jquery.validate,字段验证直到字段第一次更改和模糊时才开始,或者如果字段未被触及则在提交时开始。
一旦字段验证开始,验证器的 onkeyup 设置将控制是否对每个 keyup 事件执行验证。如果您在每次按键时对字段执行远程验证,则可能会发送过多的 ajax 消息,给客户端和服务器带来压力。
正如其他响应者提到的,您可以通过设置 validator.settings.onkeyup = false 来抑制 keyup 验证,但这会抑制所有表单字段的 onkeyup 验证,而不仅仅是那些具有远程验证规则的表单字段。我的建议是,当具有远程验证的字段具有焦点时,您抑制键盘按键,然后在模糊该字段时将其重新打开。
抑制 onkeyup 很容易。问题在于,将设置从 false 更改为 true 不会重新打开该功能。要重新打开它,您需要将其设置为 $.validator 对象的默认值中定义的函数。
如果您使用 MVC 非侵入式验证,则以下是要包含在页面上的 javascript 代码。它将涵盖您页面上的所有表单。并且一定要使用 jquery.validate 版本 1.9.0 或更高版本,因为早期版本无法在 IE 7 或 8 上正常工作(是的,我也必须支持这些用户):
MVC3 uses the jquery.validate plugin in the browser. With jquery.validate, field validation doesn't start until the first change and blur of the field, or upon submit if the field wasn't touched.
Once field validation starts for a field, the onkeyup setting of the validator controls whether validation is performed on each keyup event. If you're performing remote validation on a field with every key press it's probably sending too many ajax messages stressing both the client and server.
As the other responders mentioned, you can suppress the keyup validation by setting validator.settings.onkeyup = false, but that suppresses onkeyup validation on all form fields, not just those with a remote validation rule. What I suggest is that you suppress keyup while the field with remote validation has focus and then turn it back on when you blur the field.
Suppressing onkeyup is easy. The problem is that changing the setting from false to true does not turn the feature back on. To turn it back on you need to set it to the function defined in the defaults of the $.validator object.
Here's the javascript code to include on your page if you're using MVC unobtrusive validation. It will cover all forms on your page. And be sure to use jquery.validate version 1.9.0 or later 'cause earlier versions don't work properly with IE 7 or 8 (yeah, I have to support those users too):
我之前在使用 MVC3 和远程验证时也遇到过类似的问题。猜测您希望事件在模糊时触发,但不确定如何告诉远程验证..
希望这会有所帮助
ASP.NET 远程验证仅在模糊时进行?
I have been caught on similar problems before with MVC3 and remote validation. Guess you want the event to fire on blur but not sure how you'd tell the remote validation that..
Hopefully this will help
ASP.NET Remote Validation only on blur?
如果您不想在 keyup 上验证,而是在模糊上验证,那么这个 就是你想要的
If you don't want to validate on keyup but on blur then this is what you want
没有一种方法是合适的答案,因为它消除了在错误已经显示时重新验证 keyup 的能力。我仍然想要一个反跳功能,而不是当前在每次按键时都会点击 API 的实现。
我的解决方案是重写远程方法。
None of the approach was a suitable answer as it removes the ability to revalidate on keyup when the error is already displayed. I stll want a debounce, rather than the current implementation that hits the API on every keyup.
My solution was to override the remote method.