简体   繁体   English

Ajax.BeginForm()发布方法未返回部分视图

[英]Ajax.BeginForm() post method not returning Partial View

I have a MVC application and I'm trying to insert properties of objects. 我有一个MVC应用程序,正在尝试插入对象的属性。 For that, I made a modal popup via jQuery dialog. 为此,我通过jQuery对话框进行了模式弹出。 I don't want it interfering with other actions that the user is doing, so I made an Ajax.BeginForm. 我不希望它干扰用户正在执行的其他操作,所以我制作了一个Ajax.BeginForm。 I hoped that when I do the insert, it will close on return PartialView(), but it opens the popup View on full screen instead of closing the dialog. 我希望当我执行插入操作时,它将在返回PartialView()时关闭,但是它将以全屏方式打开弹出视图,而不是关闭对话框。 Also, it is important that the base view should be dynamic, so you can open the dialog on any page and not make a postback. 同样,重要的是基本视图应该是动态的,因此您可以在任何页面上打开对话框而不进行回发。

I've read the other similar issues and couldn't resolve my problem. 我已经阅读了其他类似的问题,但无法解决我的问题。

There are some similar issues, but none of them 有一些类似的问题,但没有一个

Please, help me to achieve the proper function if possible. 请,如果可能的话,请帮助我实现适当的功能。 Code below: 代码如下:

JS: JS:

<script>
            $(document).ready(function () {
                var url = "";
                $("#dialog-alert").dialog({
                    title: 'Error encountered!',
                    autoOpen: false,
                    resizable: false,
                    width: 350,
                    show: { effect: 'drop', direction: "up" },
                    modal: true,
                    draggable: true
                });

                if ('@TempData["msg"]' != "") {
                    $("#dialog-alert").dialog('open');
                }

                $("#lnkServer").on("click", function (e) {
                    //e.preventDefault(); //use this or return false
                    url = $(this).attr('href');
                    $('#dialog-edit').dialog({ title: "Add a new Server" });
                    $("#dialog-edit").dialog('close');
                    $("#dialog-edit").dialog('open');
                    return false;
                });

                $("#lnkIssType").on("click", function (e) {
                    //e.preventDefault(); //use this or return false
                    url = $(this).attr('href');
                    $('#dialog-edit').dialog({ title: "Add a new Issue Type" });
                    $("#dialog-edit").dialog('close');
                    $("#dialog-edit").dialog('open');
                    return false;
                });

                $("#lnkUser").on("click", function (e) {
                    //e.preventDefault(); //use this or return false
                    url = $(this).attr('href');
                    $('#dialog-edit').dialog({ title: "Add a new User" });
                    $("#dialog-edit").dialog('close');
                    $("#dialog-edit").dialog('open');
                    return false;
                });

                $("#lnkDept").on("click", function (e) {
                    //e.preventDefault(); //use this or return false
                    url = $(this).attr('href');
                    $('#dialog-edit').dialog({ title: "Add a new Department" });
                    $("#dialog-edit").dialog('close');
                    $("#dialog-edit").dialog('open');
                    return false;
                });

                $("#dialog-edit").dialog({
                    autoOpen: false,
                    resizable: false,
                    width: 400,
                    show: { effect: 'drop', direction: "up" },
                    modal: true,
                    draggable: true,
                    open: function (event, ui) {
                        //$(".ui-dialog-titlebar-close").hide();
                        $(this).load(url);
                    }
                    //buttons: {
                    //    "Cancel": function () {
                    //        $(this).dialog("close");
                    //    }
                    //}
                });
            });

        function onSuccess() {
            $("#dialog-edit").dialog('close');
        }
    </script>

Form: 形成:

<div id="container">
        @using (Ajax.BeginForm("AddDept", new AjaxOptions { OnSuccess = "onSuccess" }))
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)

            <div>
                <fieldset>
                    <div class="editor-label">
                        @Html.LabelFor(model => model.Department_Name)
                    </div>
                    <div class="editor-field">
                        @Html.TextBoxFor(model => model.Department_Name, htmlAttributes: new { @class = "form-control text-box single-line input-properties", placeholder = "Collections" })
                    </div>
                    <div class="editor-label">
                        @Html.ValidationMessageFor(model => model.Department_Name)
                    </div>

                    <input type="submit" value="Submit" class="btn btn-default btn-add-properties" />
                </fieldset>
            </div>
        }
    </div>

Controller: 控制器:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult AddDept([Bind(Include = "Department_Name")] Department @dept)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Departments.Add(@dept);
                    db.SaveChanges();
                    TempData["Msg"] = "Data has been saved successfully";
                    return PartialView();
                    //return Redirect(System.Web.HttpContext.Current.Request.UrlReferrer.PathAndQuery);
                }
            }
            catch
            {
                TempData["Msg"] = "Probably the record already exists. If not, contact Georgi Georgiev, RA Dept.";
                return PartialView();
            }
            return PartialView(@dept);
            }

My problem was probably due to that my Ajax.BeginForm popup View relied on different ActionResult in the controller compared to the Background View's. 我的问题可能是由于与背景视图相比,我的Ajax.BeginForm弹出视图依赖于控制器中不同的ActionResult。

Anyway, it turns out that I couldn't achieve my functionality without having a POST through Ajax. 无论如何,事实证明,如果不通过Ajax进行POST,就无法实现我的功能。

Here's what I did (in Layout view): 这是我所做的(在“布局”视图中):

$("#dialog-edit").dialog({
                    autoOpen: false,
                    resizable: false,
                    width: 400,
                    show: { effect: 'drop', direction: "up" },
                    modal: true,
                    draggable: true,
                    open: function (event, ui) {
                        //$(".ui-dialog-titlebar-close").hide();
                        $(this).load(url);
                    },
                    buttons: [{
                        text: "Submit",
                        "class": "btn-add-properties",
                        click: function () {
                            var form = $('form', this);
                            $.post(form.prop('action'),
                                   form.serialize(),
                                   function (response) {
                                       alert("success");
                                   })
                                   .fail(function () {
                                       alert("error");
                                   });
                            $("#dialog-edit").dialog('close');
                        }
                    }]
                });

The ajax post is the function under the dialog button. Ajax发布是对话框按钮下的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM