我有一个显示用户 Angular 色列表(例如管理员、运算符(operator)等)的 View ,以及一个弹出模式窗口的“添加”按钮,允许用户添加新 Angular 色。
在我的 Controller 中,我将此作为我的 HttpPost 方法
[HttpPost]
public ActionResult Create(RoleModel model)
{
if (ModelState.IsValid)
{
var role = new RoleDto
{
Name = model.Name,
Description = model.Description
};
var roleAdded = _rolePermissionsRepository.AddRole(role);
if (roleAdded != null)
{
//CLOSE WINDOW
}
else
{
//PRINT ERROR MSG TO WINDOW
}
}
return View();
}
如果添加到数据库成功,我想关闭模式窗口,然后刷新主索引页面上的列表。
如果在保存到数据库时出现一些错误,模式窗口应保持打开状态,并显示一些错误。
我如何实现这一目标?
这就是我在索引页面上用来弹出窗口的内容
$("#open").click(function (e) {
wnd.center();
wnd.open();
});
最佳答案
您应该返回一个 JsonResult 来告诉浏览器发生了什么。
[HttpPost]
public ActionResult Create(RoleModel model)
{
if (ModelState.IsValid)
{
var role = new RoleDto
{
Name = model.Name,
Description = model.Description
};
var roleAdded = _rolePermissionsRepository.AddRole(role);
if (roleAdded != null)
{
//CLOSE WINDOW
return Json(new { success = true });
}
else
{
return Json(new { error = "Error! Can't Save Data!" });
}
}
return Json(new { error = "Generic Error Message!" });
}
这是应该在您的 wnd 页面中运行的 javascript,如果有任何错误消息,您将显示错误消息,否则您将关闭窗口。
$('form').submit(function(e) {
e.preventDefault();
$.post(this.action, $(this).serialize(), function(response) {
if(response.error) {
alert(response.error);
}
else {
wnd.close();
}
}, 'json');
});
关于c# - 在 httppost 之后关闭模式窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13454514/