TempData 未持久化

发布于 2024-07-29 21:00:33 字数 669 浏览 4 评论 0原文

我有一个在返回视图之前设置 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小草泠泠 2024-08-05 21:00:34

我遇到了同样的问题,结果发现我们在 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 of IControllerFactory. I changed this to default, 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 the Asp.Net State Service in Windows Services.

TempData requires the use of Session State between requests.

Hope this helps someone.

任性一次 2024-08-05 21:00:33

TempData 在两个请求之间持续存在。 编辑操作中的 ReturnView 方法返回什么? 据我所知,这不是 Controller 类中定义的标准方法。 您是否在此方法中进行重定向(即返回 RedirectToRouteResult)?

此外,您的“编辑”和“删除”操作之间是否可能发生任何其他请求? 例如ajax请求。

一般来说,使用 TempData 来长期保存某些内容并不是一个好主意。 该模式通常如下:

public ActionResult Update() 
{
    // When you put something into the TempData dictionary you usually
    // redirect immediately to an action that will use the object stored
    // inside.
    TempData["something"] = "something";
    return RedirectToAction("success");
}

public ActionResult Success() 
{
    var something = TempData["something"];
    return View();
}

如果您需要将某些内容保留更长时间,则应该使用 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:

public ActionResult Update() 
{
    // When you put something into the TempData dictionary you usually
    // redirect immediately to an action that will use the object stored
    // inside.
    TempData["something"] = "something";
    return RedirectToAction("success");
}

public ActionResult Success() 
{
    var something = TempData["something"];
    return View();
}

If you need to persist something for a longer time you should use Session.

溇涏 2024-08-05 21:00:33

TempData 不起作用的另一个因素是当您的应用程序位于分布式系统下时。

Another factor of TempData not working is when your App is under a distributed system.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文