不使用 jquery 和 AJAX 在 mvc4 视图中弹出成功警报
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20279560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Success Alert popup in mvc4 View without using jquery and AJAX
提问by Joseph
Is it possible to get an alert popup on return to same view after success in mvc4 without using ajax begin form?
在 mvc4 中成功后是否可以在不使用 ajax 开始表单的情况下返回相同视图时弹出警报?
I'm trying to submit a form and on success want to show a alert box without using ajax and jquery .
我正在尝试提交一个表单,成功后希望在不使用 ajax 和 jquery 的情况下显示一个警报框。
回答by karaxuna
When you submit form, I think then you are redirecting, am i right? So you can use TempData
for this purpose:
当您提交表单时,我认为您正在重定向,对吗?因此,您可以TempData
为此目的使用:
In controller action:
在控制器动作中:
if(success)
{
TempData["AlertMessage"] = "my alert message";
return RedirectToAction("SomeAction");
}
The view which SomeAction
action returns (or in layout view):
SomeAction
操作返回的视图(或在布局视图中):
@{
var message = TempData["AlertMessage"] ?? string.Empty;
}
<script type="text/javascript">
var message = '@message';
if(message)
alert(message);
</script>
NOTE: If you are not redirecting, but returning view, just use ViewBag
instead of TempData
.
注意:如果您不是重定向,而是返回视图,只需使用ViewBag
代替TempData
。