提交时的Jquery对话框返回白色视口中的Json数据

提交时的Jquery对话框返回白色视口中的Json数据,jquery,asp.net-mvc,jquery-ui,jquery-dialog,Jquery,Asp.net Mvc,Jquery Ui,Jquery Dialog,当我按下保存按钮时,我从我的控制器返回: return Json(new { success = true }); 我的整个浏览器视口是白色的,在左上角我看到了我返回的json数据: {"success":true} 问题:如果此白色视口已修复,我必须更改什么 我的期望是检查是否成功:在客户端为true/false,并根据值关闭对话框或更改一些数据,调用对话框的站点应保留 $(document).ready(function () { // the div holds

当我按下保存按钮时,我从我的控制器返回:

return Json(new { success = true });
我的整个浏览器视口是白色的,在左上角我看到了我返回的json数据:

{"success":true}
问题:如果此白色视口已修复,我必须更改什么

我的期望是检查是否成功:在客户端为true/false,并根据值关闭对话框或更改一些数据,调用对话框的站点应保留

   $(document).ready(function () {

        // the div holds the html content
        var $dialog = $('<div></div>')  
        .dialog({
            autoOpen: false,
            title: 'This is the dialogs title',
            height: 400,
            width: 400,
            modal: true,
            resizable: false,
            hide: "fade",
            show: "fade",
            open: function (event, ui) {
                $(this).load('@Url.Action("Create")');
            },
            buttons: {
                "Save": function () {
                    var form = $('form', this);
                    $(form).submit();
                },
                "Close": function () {
                    $(this).dialog("close");                   
                }
            } // no comma
        });

        $('#CreateTemplate').click(function () {
            $dialog.dialog('open');
            // prevent the default action, e.g., following a link
            return false;
        });

    });

</script>

控制器后操作:

  [HttpPost]
        public ActionResult JsonCreate(Template template)
        {
            if (ModelState.IsValid)
            {
                _templateDataProvider.AddTemplate(template);
                return Json(new { success = true });
            }

            return Json(new { errors = GetErrorsFromModelState() });
        }

您必须执行Ajax提交,以便页面保持在相同的url上:

            "Save": function () {
                var form = $('form', this);
                //$(form).submit();
                $.post(form.prop('action'), form.serialize(), function(response){
                    //do something with response data or ignore it
                }, 'json');
            },

请重新编写$.post代码,因为对于jquery初学者(我)来说,这种风格不容易理解。我猜.success(可以在更多行中重写,对吗?因为您的ajax提交,我猜我当时使用$.ajax({data:..type:..url:..});现在它调用$.post(url,数据,回调,type),就像上的文档一样,我已经用新的ajax代码更新了我的问题:你能告诉我为什么当我到达$.ajax调用时调试器停止,并且成功==true时,我的对话框没有关闭吗?这里有些不工作…好的,我发现一个错误:我用“Template/JsonCreate”替换了这个.action,(controller/action)硬编码,调试器2号在success==true区域继续,但我的对话框尚未关闭,为什么?“this”不再是回调中的div。请改用$dialog.dialog('close')
  [HttpPost]
        public ActionResult JsonCreate(Template template)
        {
            if (ModelState.IsValid)
            {
                _templateDataProvider.AddTemplate(template);
                return Json(new { success = true });
            }

            return Json(new { errors = GetErrorsFromModelState() });
        }
            "Save": function () {
                var form = $('form', this);
                //$(form).submit();
                $.post(form.prop('action'), form.serialize(), function(response){
                    //do something with response data or ignore it
                }, 'json');
            },