jQuery UI对话验证

时间:2012-01-19 05:44:23

标签: asp.net-mvc-3 jquery-ui jquery-ui-dialog unobtrusive-validation

我知道这是一个常见的问题。我一直在寻找在这个网站和其他地方提出的各种解决方案,但不能找到适用于我的情况的东西。

这是我的剧本:

$(function () {

    $('a.editPersonLink').live("click", function (event) {
        loadDialog(this, event, '#personList');
        $.validator.unobtrusive.parse('#editPersonContainer');
    });

    $(".deleteButton").live("click", function(e) {
        e.preventDefault();
        var $btn = $(this);
        var $msg = $(this).attr("title");

        confirmDelete($msg, 
            function() {
                deleteRow($btn, $target);
            });
    });

});

function loadDialog(tag, event, target) {
    event.preventDefault();
    var $loading = $('<img src="../../Content/images/ajaxLoading.gif" alt="loading" class="ui-loading-icon">');
    var $url = $(tag).attr('href');
    var $title = $(tag).attr('title');
    var $dialog = $('<div></div>');
    $dialog.empty();
    $dialog
        .append($loading)
        .load($url)
        .dialog({
            autoOpen: false
            ,title: $title
            ,width: 800
            ,modal: true
            ,minHeight: 200
            ,show: 'slide'
            ,hide: 'clip'
        });

    $dialog.dialog( "option", "buttons", {
        "Save": function () {
            var dlg = $(this);
            $.ajax({
                url: $url,
                type: 'POST',
                data: $("#formData").serialize(),
                success: function (response) {
                    $(target).html(response);
                    dlg.dialog('close');
                    dlg.empty();
                    $("#ajaxResult").hide().html('Record saved').fadeIn(300, function () {
                        var e = this;
                        setTimeout(function () { $(e).fadeOut(400); }, 2500);
                    });
                },
                error: function (xhr) {
                    if (xhr.status == 400) 
                        dlg.html(xhr.responseText, xhr.status);     /* display validation errors in edit dialog */
                    else 
                        displayError(xhr.responseText, xhr.status); /* display other errors in separate dialog */

                }
            });
        },
        "Cancel": function() { 
            $(this).dialog("close");
            $(this).empty();
        }
    });

    $dialog.dialog('open');
};

靠近顶部我试图通过语句使表单从对话框的局部视图中识别验证:

$.validator.unobtrusive.parse('#editPersonContainer');

editPersonContainer是div的名称,包含加载到对话框中的局部视图中的数据。

底线是无法识别验证。我在错误的地方打电话给validator.unobtrusive.parse,还是我在这里错过了其他的东西?

2 个答案:

答案 0 :(得分:1)

我最终更改了我的脚本以使用here

所描述的技术

验证现在适用于我的jQuery UI对话框。

答案 1 :(得分:0)

您好我找到了同样的问题。

我在ajax调用之前将其包括在客户端验证:

if (ModelState.IsValid) {}

我用

上的完整项目撰写了一篇文章

Validation client/Server JqueryUI Dialog