[英]Ajax.BeginForm() post method not returning Partial View
我有一個MVC應用程序,正在嘗試插入對象的屬性。 為此,我通過jQuery對話框進行了模式彈出。 我不希望它干擾用戶正在執行的其他操作,所以我制作了一個Ajax.BeginForm。 我希望當我執行插入操作時,它將在返回PartialView()時關閉,但是它將以全屏方式打開彈出視圖,而不是關閉對話框。 同樣,重要的是基本視圖應該是動態的,因此您可以在任何頁面上打開對話框而不進行回發。
我已經閱讀了其他類似的問題,但無法解決我的問題。
有一些類似的問題,但沒有一個
請,如果可能的話,請幫助我實現適當的功能。 代碼如下:
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>
形成:
<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>
控制器:
[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);
}
我的問題可能是由於與背景視圖相比,我的Ajax.BeginForm彈出視圖依賴於控制器中不同的ActionResult。
無論如何,事實證明,如果不通過Ajax進行POST,就無法實現我的功能。
這是我所做的(在“布局”視圖中):
$("#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');
}
}]
});
Ajax發布是對話框按鈕下的功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.