我需要在其中一个视图中传递成功的注销消息,但我无法这样做。以下是我的代码:
无效的解决方案:
//LogController:
public ActionResult Logoff()
{
DoLogOff();
TempData["Message"] = "Success";
return RedirectToAction("Index", "Home");
}
// HomeController
public ActionResult Index()
{
return View();
}
Index CSHTML文件:
@Html.Partial("../Home/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();
}
Index CSHTML文件:
@TempData["Message"]
但我想要类似于我的第一个解决方案的东西。
TempData
?一切依赖于ASP.NET会话(因此依赖于服务器端状态)的东西都应该避免使用。 - Darin Dimitrov