如何在ASP.Net MVC中的RedirectToAction中传递tempdata [英] How to pass tempdata in RedirectToAction in ASP.Net MVC

查看:106
本文介绍了如何在ASP.Net MVC中的RedirectToAction中传递tempdata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在其中一个视图中传递一条登出成功消息,但我不能这样做.这就是我所拥有的.

I need to pass one logout successful message in one of the views but I am not able to do so. Here is what I have.

无效的解决方案:

 //LogController:
  public ActionResult Logoff()
  {
      DoLogOff();
      TempData["Message"] = "Success";
      return RedirectToAction("Index", "Home");
  }

  // HomeController
  public ActionResult Index()
  {
      return View();
  }

索引CSHTML文件:

@Html.Partial("../Home/DisplayPreview")

DisplayPreview CSHTML文件:

   @TempData["Message"] 

工作解决方案

public ActionResult Logoff()
{
     DoLogOff();
     return RedirectToAction("Index", "Home", new { message = "Logout Successful!" });
}

public ActionResult Index(string message)
{
    if (!string.IsNullOrEmpty(message))
        TempData["Message"] = message;
    return View();
}

索引CSHTML文件:

   @TempData["Message"] 

但是我想要第一个解决方案.

But I want something like my first solution.

推荐答案

在控制器中;

public ActionResult Index()
{
    ViewBag.Message = TempData["Message"];
    return View();
}
public ActionResult Logoff()
{
    DoLogOff();
    TempData["Message"] = "Success";
    return RedirectToAction("Index", "Home");
}

然后您可以在类似的视图中使用它;

Then you can use it in view like;

@ViewBag.Message

这篇关于如何在ASP.Net MVC中的RedirectToAction中传递tempdata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆