调用Action Method,关闭对话框并刷新父项

Call Action Method, close dialog and refresh parent

本文关键字:刷新 对话框 Action Method 调用      更新时间:2024-04-08

由于我是MVC5的新手,我可能对此有错误的想法。

我有一个CustomerInfo.cshtml视图,上面列出了CustomerContact。它们旁边是一个"+"图标,它将打开一个对话框窗口,允许他们添加新的CustomerContact。

在打开对话框时,我调用CustomerContactController创建GET方法(将customerId传递给它,将其设置为我的模型,并将其传递给视图)。

保存时,我调用CustomerContactController Create POST方法设置值并保存到数据库。

此时,我如何关闭对话框窗口并刷新父CustomerInfo.cshtml页面,以便新联系人出现在列表中。所有的Action方法都要求我返回一些东西。

我从CustomerInfo.cshtml视图打开对话框,如下所示:

@(Html.Kendo().Window()
    .Name("windowContact")
    .Title("New Customer Contact")
     .LoadContentFrom("Create", "CustomerContact", new { customerId = Model.Id })
    .Draggable()
    .Width(680)
    .Visible(false)
    .Events(events => events.Open("centerWindow"))
)

在对话框窗口中,我有一个剃刀形式。这是对话框的底部:

            <input type="submit" value="Save" id="btnSave" class="btn btn-primary" />
            <input type="button" value="Cancel" id="btnCancel" class="btn btn-default" />
}
<script>
    $("#btnCancel").click(function () {
        $("#windowContact").data("kendoWindow").close();
    });
</script>

以下是我目前正在做的操作,它将带我返回CustomerInfo视图,但会进行刷新。使用AJAX来顺利完成这项工作的唯一方法是什么?

return RedirectToAction("CustomerInfo", "Customer", new { id = custContact.CustomerId });

通过jQuery Ajax发布Create view的表单数据。类似:

$.ajax({
  url: '/Create/CustomerContact',
  type: 'post',
  contentType: "application/x-www-form-urlencoded",
  data: $form.serialize(),
  success: function (data, status) {
    // here close the window and refresh parent  
    // if your new window is opened in iframe  
    window.parent.location.reload();
    $("#windowContact").data("kendoWindow").close();
    // if your new window is opened by window.open() method.
    window.opener.location.reload();
    self.close();
  },
  error: function (xhr, desc, err) {
    alert("Desc: " + desc + "'nErr:" + err);
  }
});

希望它对你有用,谢谢。