How to use asp.net mvc 3 jquery validate with a jquery dialog that does an ajax submit?

耗尽温柔 提交于 2019-11-30 20:31:03

Something like this should do the trick:

    $("#my-button").click(function (event) {
        event.preventDefault();

        $("#my-form").validate();

        if ($("#my-form").valid()) {
            // ...
        }
    });

The issue may be due to elements being added to the DOM after the unobtrusive script loads initially. You can force it to attach validation handlers the new elements by calling $.validator.unobtrusive.parse("form") after creating the dialog.

$("<div></div>").dialog(...)
$.validator.unobtrusive.parse("form")

This solved our similar issue although we display the form within the dialog.

$("#my-button").click(function (event) {
    event.preventDefault();

    $.validator.unobtrusive.parse("#my-form");

    if ($("#my-form").valid()) {
        // ...
    }
});

This can be done using a feature of ASP.NET MVC called Remote Validation. All you have to do is decorate the property you want validated with a [Remote] attribute passing the name of the method that does the validation and the controller on which it exists.

you can find an example on how How to: Implement Remote Validation in ASP.NET MVC

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