在 ASP.NET MVC 3 中调用 RedirectToAction() 后会话状态被擦除
我使用一个简单的序列:
- 在 [HttpGet] 方法中设置会话状态。
- 使用 [HttpPost] 方法中的 RedirectToAction() 重定向到另一个操作。
- 想要获取目的地中该会话状态的值。
问题:
如果用户点击“表”视图上的“提交”按钮,会话中的所有数据都会被清除,并且我无法在目标操作(即“表”)中获取它们。这是代码:
[HttpGet]
public ActionResult Edit(string TableName, int RowID, NavigationControl nav)
{
if (nav != null) Session["NavigationData"] = nav;
myService svc = new myService (_repository);
EditViewModel model = new EditViewModel();
model.TableDefinitions = svc.GetTableDefinition(TableName);
model.RowData = svc.GetRowData(model.TableDefinitions.Name, RowID);
return View(model);
}
[HttpPost]
public ActionResult Edit(EditViewModel model)
{
MyService svc = new MyService (_repository);
svc.SaveRowData(model.TableDefinitions.Name, model.RowData);
return RedirectToAction("Table");
}
public ActionResult Table(string TableName)
{
myService svc = new myService (_repository);
TableViewModel model = new TableViewModel();
model.TableDefinition = svc.GetTableDefinition(TableName);
NavigationControl nav = (NavigationControl)Session["NavigationData"];
if (nav != null)
{
model.NavigationControl = nav;
}
return View(model);
}
当用户通过 return RedirectToAction("Table") 到达它时,Session["NavigationData"] 始终为 null。 如果用户在“编辑”视图上点击 HTML 链接,Session["NavigationData"] 可以在“Table”方法中恢复其值!
知道发生了什么事吗? 谁删除了Session状态?!
I use a simple sequences:
- Set a Session State in [HttpGet] method.
- Redirect to another action using RedirectToAction() in [HttpPost] method.
- Want to get the value of that Session State, in the destination.
Problem:
If user hits "submit" button on my "Table" view, all the data inside session got cleared and I can't get them in the destination action (which is "Table"). Here is the code:
[HttpGet]
public ActionResult Edit(string TableName, int RowID, NavigationControl nav)
{
if (nav != null) Session["NavigationData"] = nav;
myService svc = new myService (_repository);
EditViewModel model = new EditViewModel();
model.TableDefinitions = svc.GetTableDefinition(TableName);
model.RowData = svc.GetRowData(model.TableDefinitions.Name, RowID);
return View(model);
}
[HttpPost]
public ActionResult Edit(EditViewModel model)
{
MyService svc = new MyService (_repository);
svc.SaveRowData(model.TableDefinitions.Name, model.RowData);
return RedirectToAction("Table");
}
public ActionResult Table(string TableName)
{
myService svc = new myService (_repository);
TableViewModel model = new TableViewModel();
model.TableDefinition = svc.GetTableDefinition(TableName);
NavigationControl nav = (NavigationControl)Session["NavigationData"];
if (nav != null)
{
model.NavigationControl = nav;
}
return View(model);
}
and Session["NavigationData"] is always null when user reaches it via: return RedirectToAction("Table").
If user hits an HTML link on "Edit" View, Session["NavigationData"] can restore its value in "Table" method!
Any idea about what's going on?
Who deletes the Session state?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的浏览器 cookie 已关闭,但状态未设置为无 cookie。
My browser cookie was off but state was not set to cookie-less.