asp.net-mvc 将视图数据保留在 RedirectToAction 上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1226329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:45:39  来源:igfitidea点击:

keep viewdata on RedirectToAction

asp.net-mvcviewdataredirecttoaction

提问by Thomas Stock

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateUser([Bind(Exclude = "Id")] User user)
{
        ...
        db.SubmitChanges();
        ViewData["info"] = "The account has been created.";
        return RedirectToAction("Index", "Admin");
}

This doesnt keep the "info" text in the viewdata after the redirectToAction. How would I get around this issue in the most elegant way?

在redirectToAction 之后,这不会在viewdata 中保留“信息”文本。我将如何以最优雅的方式解决这个问题?

My current idea is to put the stuff from the Index controlleraction in a [NonAction] and call that method from both the Index action and in the CreateUser action, but I have a feeling there must be a better way.

我目前的想法是将 Index controlleraction 中的内容放在 [NonAction] 中,并从 Index 操作和 CreateUser 操作中调用该方法,但我觉得必须有更好的方法。

Thanks.

谢谢。

回答by Mathias F

You can use TempData.

您可以使用TempData.

TempData["info"] = "The account has been created.".

TempData["info"] = "The account has been created.".

TempDataexists exactly for this situation. It uses Session as storage, but it will not be around after the second response.

TempData正是针对这种情况而存在的。它使用 Session 作为存储,但在第二次响应后它不会存在。

From MSDN:

来自 MSDN:

A typical use for a TempDataDictionary object is to pass data from an action method when it redirects to another action method. For example, an action method might store information about an error in the controller's TempData property (which returns a TempDataDictionary object) before it calls the RedirectToAction method. The next action method can then handle the error and render a view that displays an error message.

TempDataDictionary 对象的典型用途是在重定向到另一个操作方法时从操作方法传递数据。例如,操作方法可能会在调用 RedirectToAction 方法之前在控制器的 TempData 属性(返回 TempDataDictionary 对象)中存储有关错误的信息。然后下一个操作方法可以处理错误并呈现显示错误消息的视图。

回答by eu-ge-ne

Use ViewDataif your data should be accessible in Viewduring "this" request. Use `TempData' if your data is for "next" request (for example POST-REDIRECT-GET design pattern).

ViewData如果View在“此”请求期间应该可以访问您的数据,请使用。如果您的数据用于“下一个”请求(例如POST-REDIRECT-GET 设计模式),请使用“TempData” 。

回答by Leon

If you need this more than once, a nice workaround would be creating ActionFilterAttributes which export/import the tempdata to viewdata and vice-versa. You can pass your ModelState in this way very nicely as well (demonstrated here- #13). With a few adjustments to that piece of code you would have a clean solution, I think.

如果您不止一次需要这个,一个很好的解决方法是创建 ActionFilterAttributes,它将临时数据导出/导入到视图数据,反之亦然。您也可以通过这种方式非常好地传递您的 ModelState(在此处演示- #13)。我认为,通过对该段代码进行一些调整,您将获得一个干净的解决方案。

回答by Christ A

You could use the TempDatacontroller property, but it has the disadvantage that it uses the session storage in the background. This means that you'll have extra work getting it to function on a web farm and you'll need to have sessions enabled in your application in the first place.

您可以使用TempData控制器属性,但它的缺点是它在后台使用会话存储。这意味着您需要做额外的工作才能让它在 Web 场上运行,而且您首先需要在应用程序中启用会话。

An alternative is to use cookies if you only need to transport a short message. This does require proper encryption of the cookie. Not relying on the TempDataproperty also allows you to set messages in a non MVC context, for example in a classic ASHX page.

如果您只需要传输一条短消息,另一种方法是使用 cookie。这确实需要对 cookie 进行适当的加密。不依赖TempData属性还允许您在非 MVC 上下文中设置消息,例如在经典的 ASHX 页面中。

Take a look at FlashMessagewhich can save you some work implementing this yourself.

看看FlashMessage,它可以为您节省一些自己实现的工作。

回答by Novox

Since TempData appears to use storage, and any form of ITempDataProvider that is not "in-process", requires the object to be Serializable, TempData seems woefully inadequate in web farm situations... (ViewDataDictionary isn't itself serializable...) Does anyone have any suggestions for this?

由于 TempData 似乎使用存储,并且任何形式的 ITempDataProvider 不是“进程内”,都要求对象是可序列化的,TempData 在网络农场情况下似乎严重不足......(ViewDataDictionary 本身不是可序列化的......)有没有人对此有任何建议?