ASP.NET MVC2 - hook into client side validation

人走茶凉 提交于 2019-12-11 16:05:44

问题


I want to trigger some custom code when the client side errors are updated using ASP.NET MVC2 client side validation. I've tracked down this function which I want to hook into:

Sys.Mvc.FormContext.prototype = {

    // ...

    _displayError: function Sys_Mvc_FormContext$_displayError() {
        if (this._validationSummaryElement) {
            if (this._validationSummaryULElement) {
                Sys.Mvc._validationUtil.removeAllChildren(this._validationSummaryULElement);
                for (var i = 0; i < this._errors.length; i++) {
                    var liElement = document.createElement('li');
                    Sys.Mvc._validationUtil.setInnerText(liElement, this._errors[i]);
                    this._validationSummaryULElement.appendChild(liElement);
                }
            }
            Sys.UI.DomElement.removeCssClass(this._validationSummaryElement, Sys.Mvc.FormContext._validationSummaryValidCss);
            Sys.UI.DomElement.addCssClass(this._validationSummaryElement, Sys.Mvc.FormContext._validationSummaryErrorCss);
        }
    },

    // ...

}    

How can I override this function such that my code can

  • call the original function
  • then do some other work

回答1:


Found it.

<script type="text/javascript">
    $(function () {
        var old_displayError = Sys.Mvc.FormContext.prototype._displayError;
        Sys.Mvc.FormContext.prototype._displayError = function () {
            old_displayError.apply(this);
            // do other stuff here
        }
    });
</script>

I'm not used to overriding prototype functions, so I was stumped for a bit.



来源:https://stackoverflow.com/questions/3917212/asp-net-mvc2-hook-into-client-side-validation

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