在控制器中使用 RedirectToRoute(“Route”) 时,ViewData[“key”] 不可用
我正在开发我的第一个基于 MVC3 的应用程序,但仍然是一个新手。
问题是我将一些消息放入 ViewData 字典中的控制器中以显示在视图页面上,但就我使用的而言 return View("Index");消息在视图中正确接收,但是当我更改控制器以通过 RedirectToRoute("Default") 返回时。ViewData[] 字典显示空值。
我想使用 RedirectToRoute("Default") 更改 url 地址..有什么方法可以将 ViewData[] 与 RedirectToRoute("Default") 一起使用吗?
public ActionResult Logout()
{
Session.Contents.Remove("usrnme");
Session.Contents.Remove("usrId");
ViewData["mainMessage"] = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
//return View("Index");
return RedirectToRoute("Default");
}
I am working over my first application over MVC3 and still kind of a newbie in it.
Problem is am putting some message in my controller in ViewData dictionary to be shown over the viewpage, but as far as I am using return View("Index"); the meesage is received in view correctly but when I change controller to return through RedirectToRoute("Default").. the ViewData[] dictionary displays null value.
I want to use RedirectToRoute("Default") to change the url address .. Is there any way I can also use ViewData[] with RedirectToRoute("Default")?
public ActionResult Logout()
{
Session.Contents.Remove("usrnme");
Session.Contents.Remove("usrId");
ViewData["mainMessage"] = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
//return View("Index");
return RedirectToRoute("Default");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
并使用默认值
Try
and in Default
因为此操作没有返回“视图”(似乎您的注销方法不在处理“默认”路由的任何控制器上),所以您无法将 ViewData 分配给它。相反,请在映射到“默认”路由的控制器的操作中设置 ViewData。
Because there is no "view" being returned on this action (Seems like your logout method is not on whichever controller handles the "Default" route), you can't assign the ViewData to it. Instead, set the ViewData in the Action of the Controller that is mapped to the "Default" route.