Asp.net mvc 如何将thw Viewdata传递到控制器中的所有视图?
我有一个选择值的下拉列表Asp.net mvc 如何将thw Viewdata传递到控制器中的所有视图?,asp.net-mvc,Asp.net Mvc,我有一个选择值的下拉列表 [AcceptVerbs(HttpVerbs.Post)] public ActionResult Screenname(FormCollection collection) { Viewdata["screenname"] = collection[0]; return RedirectToAction("Index", new { ScreenName = ViewData["screenname"] });
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Screenname(FormCollection collection)
{
Viewdata["screenname"] = collection[0];
return RedirectToAction("Index", new { ScreenName = ViewData["screenname"] });
}
然后我想在其他类似的操作中访问此ViewData
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection collection, string screenname)
{
try
{
/// thats my dataobject which creates
DataObj.SaveData(Guid.Empty, collection, screenname);
return RedirectToAction("Index", new { ScreenName = ViewData["screenname"] });
}
catch
{
return View("Error");
}
}
其中索引看起来像这样
public ActionResult Index(string ScreenName)
{
///thats my list
GetTable = new GetDataTable(ScreenName);
return View(GetTable);
}
首先,当我选择值并正确执行索引时。。。。但是当我再次尝试访问viewdata时,它不包含该值,因此任何人都可以提供帮助。。。
或者使用另一种方法来保存和检索数据。这实际上已经在这里经常提到了。目前的解决方案是在使用RedirectToAction()之前使用TempData保存所需的数据 如果你搜索“RedirectToAction”,你会发现很多关于这个主题的帖子
框架的下一个正式版本将解决这个问题。视图数据对象是特定于正在执行的特定操作的。要在操作之间传递数据,请使用
TempData
。更多关于MSDN的信息
您还可以通过属性直接写入会话状态。我使用一个视图从用户获取数据,然后将其保存到一个静态变量,然后使用此变量将数据传递给所有其他视图
无论如何谢谢即使我使用tempdata,也无法访问数据。它仍然是空的。我已经试过了…还有其他选择吗?另外,我还有几个动作想传递数据,所以请大家帮忙!!!解决方案每个用户都将获得相同的静态变量集,如果另一个用户在第二个用户重定向之间访问该页面,则第二个用户将获得第一个用户的值。任何人都不要使用此选项。不要使用当前接受的答案(静态变量传递)。