Event hooks for MVC unobtrusive remote validation

核能气质少年 提交于 2019-12-07 05:58:17

问题


Is there a way to hook into MVC3 unobtrusive remote validation on the client, before data is submitted to the remote method, and after a result is returned from the remote method?

I am trying to tell a remote validator to only perform validation if the submit button is clicked. The normal behavior is to validate several times, such as during keypress, blur etc. This is no good, as the validation method in question needs to call a non-idempotent operation. I need to make sure that method is only invoked if the user has clicked the submit button.

If I could hook into a before event, I could set a field in the form that flags the submit button as being clicked or not. However I would need to reset this flag after the remote method returns a validation result.

Any other suggestions? This is for validating a password using the ASP.NET Membership Provider's Membership.ValidateUser(string username, string password) method. This method will increment the FailedPasswordAttemtCount each time an invalid password is sent, so I don't want it to execute during blur, keypress, etc.


回答1:


You could override the default validation options for the current page:

$.validator.setDefaults({
    onkeyup: false,
    onfocusout: false,
    onsubmit: true
});



回答2:


The solution above didn't resolve this for me. Somehow, the validator setting was not being set. So, I implemented the following and it worked:

$(function () {
    var settings = $.data($('form')[0], 'validator').settings;
    settings.onkeyup = false;
});


来源:https://stackoverflow.com/questions/10414244/event-hooks-for-mvc-unobtrusive-remote-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!