jquery - MVC 与 Ajax 和 jQuery 表单提交问题

标签 jquery ajax asp.net-mvc

我正在使用 MVC 4 并提交一个 Ajax 表单,我已经验证该表单正在成功更新模型数据库。我正在向用户显示一个 jQuery 对话框,用户可以编辑对话框中的表单字段,然后更新或取消。

一切正常,直到我更新表单。我没有关闭 jQuery 对话框,而是转储到一个空白页面,其中包含来自 Controller 的 Json 返回值。 有什么想法吗?

查看:

@using (Ajax.BeginForm("Edit", "References", new AjaxOptions 
{
    InsertionMode = InsertionMode.Replace,
    HttpMethod = "POST",
    OnSuccess = "updateSuccess"
}, new { @id = "updateCarForm" }))
{
//etcetera

Controller :

public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ReferencesData referencesData = db.ReferencesDatas.Find(id);
            if (referencesData == null)
            {
                return HttpNotFound();
            }
            return PartialView(referencesData);
        }

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "make, model, colour")] ReferencesData referencesData)
        {
            if (ModelState.IsValid)
            {
                db.Entry(referencesData).State = EntityState.Modified;
                db.SaveChanges();
                return Json(JsonResponseFactory.SuccessResponse("Woohoo"), JsonRequestBehavior.DenyGet);
            }
            else
            {
                return Json(JsonResponseFactory.ErrorResponse("Please review your form"), JsonRequestBehavior.DenyGet);
            }
        }

Javascript:

<div id="updateDialog" title="Update Car"></div>
    <script type="text/javascript">
        var linkObj;
        $(function () {
            $(".editLink").click(function () {
                //change the title of the dialog
                linkObj = $(this);
                var opt = {
                    autoOpen: false,
                    width: 1145,
                    height: 600,
                    resizable: false,
                    modal: true,
                    title: 'Edit Reference',
                    buttons: {
                        "Update": function () {
                            $("#update-message").html(''); //make sure there is nothing on the message before we continue
                            $("#updateCarForm").submit();
                        },
                        "Cancel": function () {
                            $(this).dialog("close");
                        }
                    }
                };

                var dialogDiv = $("#updateDialog").dialog(opt);
                var viewUrl = linkObj.attr('href');
                $.get(viewUrl, function (data) {
                    dialogDiv.html(data);
                    //validation
                    var $form = $("#updateCarForm");
                    // Unbind existing validation
                    $form.unbind();
                    $form.data("validator", null);
                    // Check document for changes
                    $.validator.unobtrusive.parse(document);
                    // Re add validation with changes
                    $form.validate($form.data("unobtrusiveValidation").options);
                    //open dialog
                    dialogDiv.dialog('open');
                });
                return false;
            });

        });


        function updateSuccess(data) {
            if (data.Success == true) {
                //we update the table's info
                var parent = linkObj.closest("tr");
                parent.find(".carName").html(data.Object.Name);
                parent.find(".carDescription").html(data.Object.Description);
                //now we can close the dialog
                $('#updateDialog').dialog('close');
                //twitter type notification
                $('#commonMessage').html("Update Complete");
                $('#commonMessage').delay(400).slideDown(400).delay(3000).slideUp(400);
            }
            else {
                $("#update-message").html(data.ErrorMessage);
                $("#update-message").show();
            }
        }

    </script>

最佳答案

您需要在 JS 中捕获提交。

$('form').submit(function(){
   //serialize form
   $.post(url,{formData}function(json){
      //do what you want with the json
   })
})

你拥有它的方式会改变 View ,因为当你做一个简单的表单提交时,它想要改变它的 View 以及它得到的结果。

关于jquery - MVC 与 Ajax 和 jQuery 表单提交问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28199781/

相关文章:

javascript - ViewModel 当前状态到 JSON

javascript - 如何在 javascript 中将对象的普通数组转换为多级数组?

JavaScript/jQuery : Animated Cursor/Light Effect

jQuery - iframe 内的脚本标记无法更新 iframe 的内容

javascript - 如何离线和在线运行 Web 应用程序(在 .NET 中)

javascript - 如何将值从表单发送到 cshtml 中的 Javascript 函数?

javascript - 使用 jquery 日历时无法检测到输入

javascript - jwplayer 嵌入 sitefinity 时如何初始化?

javascript - 关于AJAX、FORM和PHP

asp.net-mvc - 向 ASP MVC 添加 Controller 工厂