我有一个MVC应用程序,我正在尝试插入某些对象的属性。为此,我通过jQuery对话框进行了模态弹出。我不希望它干扰用户正在做的其他操作,所以我创建了一个Ajax.BeginForm。我希望当我执行插入时,它将在返回PartialView()时关闭,但它会在全屏幕上打开弹出视图而不是关闭对话框。一位朋友建议返回Json,但仍然会在弹出网址(/ AddDept)上返回它,而不是将其返回到当前网址(不重新加载)。
基本视图应该是动态的,因此您可以在任何页面上打开对话框,而不是在提交时重新加载。
我已阅读其他类似问题但无法解决我的问题。
请尽可能帮助我实现正常的功能。代码如下:
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) {
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) {
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) {
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) {
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 Json(TempData["Msg"]);
//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 Json(TempData["Msg"]);
}
return Json(@dept);
}
答案 0 :(得分:0)
我的问题可能是由于我的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 post是对话框按钮下的功能。