admin管理员组文章数量:1431720
I have a view that displays a list of user roles (e.g., administrator, operator, etc) and an "Add" button that pops up a modal window, allowing the user to add a new role.
In my controller, I have this as my HttpPost method
[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();
}
If the add to the DB is successful, I would like to close the modal window, and then refresh the list on my main index page.
If there's some error while persisting to the DB, the modal window should remain open, and show some error.
How do I achieve that?
This is what I'm using on my Index page to pop up the window
$("#open").click(function (e) {
wnd.center();
wnd.open();
});
I have a view that displays a list of user roles (e.g., administrator, operator, etc) and an "Add" button that pops up a modal window, allowing the user to add a new role.
In my controller, I have this as my HttpPost method
[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();
}
If the add to the DB is successful, I would like to close the modal window, and then refresh the list on my main index page.
If there's some error while persisting to the DB, the modal window should remain open, and show some error.
How do I achieve that?
This is what I'm using on my Index page to pop up the window
$("#open").click(function (e) {
wnd.center();
wnd.open();
});
Share
Improve this question
edited Nov 19, 2012 at 13:11
tereško
58.5k25 gold badges100 silver badges150 bronze badges
asked Nov 19, 2012 at 13:10
Null ReferenceNull Reference
11.4k41 gold badges111 silver badges187 bronze badges
2
- What's wnd? Is it a jQuery UI dialog or what? – deerchao Commented Nov 19, 2012 at 13:13
- I'm using kendo UI for my modal pop, but I believe the concept should be similar? – Null Reference Commented Nov 19, 2012 at 13:17
1 Answer
Reset to default 3You should return a JsonResult to tell the browser what happened.
[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!" });
}
Here is the javascript that should run in your wnd page, you show the error message if there is any, otherwise you close the window.
$('form').submit(function(e) {
e.preventDefault();
$.post(this.action, $(this).serialize(), function(response) {
if(response.error) {
alert(response.error);
}
else {
wnd.close();
}
}, 'json');
});
本文标签: cClosing a modal window after httppostStack Overflow
版权声明:本文标题:c# - Closing a modal window after httppost - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745492212a2660650.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论