不使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:05:32  来源:igfitidea点击:

Success Alert popup in mvc4 View without using jquery and AJAX

asp.net-mvc-4jqueryrazor

提问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 TempDatafor this purpose:

当您提交表单时,我认为您正在重定向,对吗?因此,您可以TempData为此目的使用:

In controller action:

在控制器动作中:

if(success)
{
    TempData["AlertMessage"] = "my alert message";
    return RedirectToAction("SomeAction");
}

The view which SomeActionaction 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 ViewBaginstead of TempData.

注意:如果您不是重定向,而是返回视图,只需使用ViewBag代替TempData