TempData 未持久化
我有一个在返回视图之前设置 TempData 的控制器,
public ActionResult Edit(int id, int? order)
{
Route route = new Route();
// Do Work
TempData["Route"] = route;
return View(new FormViewModel(route, obj1, obj2));
}
该视图包含一个带有链接的部分视图,该链接转到另一个名为删除的操作方法,删除链接的代码是:
<%= Html.ActionLink("Delete", "Delete", new { order = item.Order })%>
删除操作方法的代码是
public ActionResult Delete(int order)
{
Route route = (Route)TempData["Route"];
// Do Work
}
:当我尝试获取 TempData["Route"]; 时,我遇到了问题 从删除操作方法返回 null。
我想知道问题是否在于这是一个 Get 而不是 Post? 如果是这样,我如何从表单中发布到删除操作方法?
I have a controller that sets TempData before returning a view,
public ActionResult Edit(int id, int? order)
{
Route route = new Route();
// Do Work
TempData["Route"] = route;
return View(new FormViewModel(route, obj1, obj2));
}
This view contians a partial view with a link which goes to another action method called delete, the code for the delete link is:
<%= Html.ActionLink("Delete", "Delete", new { order = item.Order })%>
The code for the Delete action method is:
public ActionResult Delete(int order)
{
Route route = (Route)TempData["Route"];
// Do Work
}
The problem that I'm having is when I try to get TempData["Route"]; from the Delete action method is returning null.
I'm wondering if the issue is that this is a Get and not a Post? If so how can I do a Post to the Delete ActionMethod from within my form?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题,结果发现我们在
IControllerFactory
的实现中设置了SessionStateBehavior
readonly
。 我将其更改为默认值,然后出现了与会话状态不可用以及注册表项相关的后续错误...该错误已在我的本地计算机上通过启动 Asp. Windows 服务中的网络状态服务。TempData 需要在请求之间使用会话状态。
希望这对某人有帮助。
I had the same issue, and it turned out that we had made our
SessionStateBehavior
readonly
, in an implementation ofIControllerFactory
. I changed this todefault
, and then got a subsequent error relating to Session State not being available and something about Registry keys... this error was solved on my local machine by starting theAsp.Net State Service
in Windows Services.TempData requires the use of Session State between requests.
Hope this helps someone.
TempData 在两个请求之间持续存在。 编辑操作中的 ReturnView 方法返回什么? 据我所知,这不是 Controller 类中定义的标准方法。 您是否在此方法中进行重定向(即返回 RedirectToRouteResult)?
此外,您的“编辑”和“删除”操作之间是否可能发生任何其他请求? 例如ajax请求。
一般来说,使用 TempData 来长期保存某些内容并不是一个好主意。 该模式通常如下:
如果您需要将某些内容保留更长时间,则应该使用 Session。
TempData persist between two requests. What does the ReturnView method in your Edit action returns? As far as I can tell it is not a standard method defined in the Controller class. Are you redirecting in this method (i.e. returning a RedirectToRouteResult)?
Also are there any other requests that could occur between your Edit and Delete actions? For example ajax requests.
Generally it is not a good idea to use TempData to persist something for a long time. The pattern usually is the following:
If you need to persist something for a longer time you should use Session.
TempData 不起作用的另一个因素是当您的应用程序位于分布式系统下时。
Another factor of TempData not working is when your App is under a distributed system.