如何在单个控制器中的操作之间共享数据
所以我有这个:
public ActionResult ViewForum(int ForumID)
{
_frmSrv = new ForumService();
_roleSrv = new RoleService();
//_userAccess = new UserAccess();
var modelView = _frmSrv.GetForumIndexView(ForumID);
TempData[CurrentUser.Name] = _roleSrv.GetUserAccessForForum(CurrentUser, ForumID);
modelView.UserAccess = TempData[CurrentUser.Name] as UserAccess;
return View(modelView);
}
还有这个:
public ActionResult ViewThread(int ThreadID)
{
_postSrv = new PostService();
var modelView = _postSrv.GetThreadIndexView(ThreadID);
modelView.UserAccess = TempData[CurrentUser.Name] as UserAccess;
return View(modelView);
}
您可能已经猜到我想将 UserAccess 从一个操作传递到另一个操作。 有办法做到吗?
So I have this:
public ActionResult ViewForum(int ForumID)
{
_frmSrv = new ForumService();
_roleSrv = new RoleService();
//_userAccess = new UserAccess();
var modelView = _frmSrv.GetForumIndexView(ForumID);
TempData[CurrentUser.Name] = _roleSrv.GetUserAccessForForum(CurrentUser, ForumID);
modelView.UserAccess = TempData[CurrentUser.Name] as UserAccess;
return View(modelView);
}
and this:
public ActionResult ViewThread(int ThreadID)
{
_postSrv = new PostService();
var modelView = _postSrv.GetThreadIndexView(ThreadID);
modelView.UserAccess = TempData[CurrentUser.Name] as UserAccess;
return View(modelView);
}
You probaly already guess I want to pass UserAccess from one action to another.
Is there a way to do it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ASP.NET MVC 中的操作是无状态的,这意味着操作需要传递每个请求所需的所有信息。您可以执行以下操作:
如果可以仅使用 ThreadID 获取 ForumID,则无需更改方法签名。
Actions in ASP.NET MVC are stateless which means that actions need to be passed all the information it needs for every request. You can do the following:
If it's possible to get the ForumID with only the ThreadID you don't need to change the method signature.
asp.net mvc 仍然是 asp.net,您可以使用会话、cookie 等来执行此操作
asp.net mvc is still asp.net, you can use sessions, cookies etc to do this