Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在jquery弹出窗口中进行mvc服务器端验证_Jquery_Asp.net Mvc 3 - Fatal编程技术网

如何在jquery弹出窗口中进行mvc服务器端验证

如何在jquery弹出窗口中进行mvc服务器端验证,jquery,asp.net-mvc-3,Jquery,Asp.net Mvc 3,iam使用mvc,这里将我的创建视图填充为弹出窗口,我能够获得我在模型中给出的验证,但是在控制器端写入的“名称是否存在”等验证不会在弹出窗口中执行,而是通过关闭弹出窗口来显示 控制器端使用以下代码检查登录名是否存在 if (db.login.Count(l => l.Name== loginname_create) > 0) { ModelState.AddModelError("loginname", "name alrea

iam使用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;
});
我想填充我在控制器中给出的验证,以显示在弹出对话框中。

我使用扩展:

/// <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);
                    }
                });
            }
        });
    }
})($);

希望有帮助。

@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");
            }
        }
这样,您就可以进行服务器端验证。希望这有帮助

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");
            }
        }