【问题标题】:Jquery dialog on submit returns Json data in white viewport提交时的 Jquery 对话框在白色视口中返回 Json 数据
【发布时间】:2012-05-20 18:57:31
【问题描述】:

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

return Json(new { success = true });

我的整个浏览器视口是白色的,在左上角我看到我返回的 json 数据:

{"success":true}

问题:这个白色视口是固定的,我需要改变什么?

我的期望是检查是否成功:客户端的真/假,取决于我关闭对话框或更改一些数据的值,调用对话框的站点应该保留。

   $(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>

创建视图:

@using (Html.BeginForm("JsonCreate", "Template"))
{ 
    <p class="editor-label">@Html.LabelFor(model => model.Name)</p>
    <p class="editor-field">@Html.EditorFor(model => model.Name)</p>
    <p class="editor-field">@Html.ValidationMessageFor(model => model.Name)</p>    
}

更新

"Save": function () {
                    var form = $('form', this);
                    debugger;
                    $.ajax({
                        url: this.action,
                        type: 'POST',
                        data: form.serialize(),
                        dataType: 'json',
                        success: function (result) {
                            debugger;   
                            if (result.success) {
                                $(this).dialog("close");
                                // Update UI
                            }
                            else {
                                // Reload the dialog to show model errors                    
                                $(dialog).html(result);
                            } // else end
                        } // success end
                    }); // ajax post end

                },

控制器 POST 操作:

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

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

【问题讨论】:

    标签: jquery asp.net-mvc jquery-ui jquery-dialog


    【解决方案1】:

    您必须进行 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, data, callback, type),就像api.jquery.com/jQuery.post的文档一样
    • 我已经用新的 ajax 代码更新了我的问题:你能告诉我为什么调试器在我到达 $.ajax 调用时停止并且成功 == true 我的对话框没有关闭吗?有些东西在这里不起作用......
    • 好的我发现一个错误:我用'Template/JsonCreate'替换了this.action,(控制器/动作)硬编码,它的调试器2号继续成功==真正的区域,但我的对话框是还没关门,为什么?
    • "this" 不再是回调中的 div。改用 $dialog.dialog('close')
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    相关资源
    最近更新 更多