0

我正在使用 mvc,这里将我的创建视图填充为弹出窗口,我能够获得我在模型中给出的验证,但是在控制器端编写的“名称是否存在”之类的验证没有在弹出窗口中执行,它们显示为关闭弹出窗口。

控制器端正在使用以下代码检查登录名是否存在

 if (db.login.Count(l => l.Name== loginname_create) > 0)
            {
                ModelState.AddModelError("loginname", "name already exists");
            }

jquery弹出对话框代码是

$("#dialog").click(function (e) {
    e.preventDefault();
    var url = $(this).attr('href');
    $("#dialog-create").dialog({
       height:300,
        width: 400,
        show: { effect: 'drop', direction: "up" },
        modal: true,
        open: function (event, ui) {
            $(this).load(url);
           //$.validate.unobtrusive.parse("myform");

            }, close: function (event, ui) {
                $(this).dialog('close');
        }
    });
    $("#dialog-create").dialog('open'); return false;
});

我想填充我在控制器中提供的验证,以显示在弹出对话框中。

4

2 回答 2

0

@Brad,如果您有按钮,我不知道您的弹出表单是否有按钮,那么您可以在对话框初始化代码中这样编写,

buttons: {
            OK: function () {
                //here make a ajax call to check NAME exists or not
                if(name exists)
                {
                   //Success message
                   $(this).dialog("close");
                } 
                else
                {
                   //Give error message

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

像这样,您可以进行服务器端验证。希望这可以帮助。

于 2013-08-22T13:19:16.260 回答
0

我使用这个扩展:

/// <reference path="jquery-1.4.4.js" />
/// <reference path="jquery.validate.js" />
/// <reference path="jquery.validate.unobtrusive.js" />
// Original from: http://xhalent.wordpress.com/2011/01/24/applying-unobtrusive-validation-to-dynamic-content/
(function ($) {
    $.validator.unobtrusive.parseDynamicContent = function (selector) {
        //use the normal unobstrusive.parse method
        $.validator.unobtrusive.parse(selector);

        //get the relevant form
        var form = $(selector).first().closest('form');

        //get the collections of unobstrusive validators, and jquery validators
        //and compare the two
        var unobtrusiveValidation = form.data('unobtrusiveValidation');
        var validator = form.validate();

        $.each(unobtrusiveValidation.options.rules, function (elname, elrules) {
            if (validator.settings.rules[elname] == undefined) {
                var args = {};
                $.extend(args, elrules);
                args.messages = unobtrusiveValidation.options.messages[elname];
                //edit:use quoted strings for the name selector
                $("[name='" + elname + "']").rules("add", args);
            } else {
                $.each(elrules, function (rulename, data) {
                    if (validator.settings.rules[elname][rulename] == undefined) {
                        var args = {};
                        args[rulename] = data;
                        args.messages = unobtrusiveValidation.options.messages[elname][rulename];
                        //edit:use quoted strings for the name selector
                        $("[name='" + elname + "']").rules("add", args);
                    }
                });
            }
        });
    }
})($);

然后,在您加载内容后,只需:

$.validator.unobtrusive.parseDynamicContent('#myform');

希望能帮助到你。

于 2013-08-22T05:51:50.373 回答