asp.net-mvc – 保持viewdata在RedirectToAction

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

这不会保持viewData中的“info”文本在redirectToAction之后。
我如何以最优雅的方式解决这个问题?

我当前的想法是把来自Index controlleraction的东西放在一个[NonAction]中,并从Index操作和createuser操作中调用方法,但我有一种感觉,必须有一个更好的方法

谢谢。

解决方法

您可以使用TempData。

TempData [“info”] =“帐户已创建。”。

TempData正好存在这种情况。它使用Session作为存储,但它不会在第二个响应后。

从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.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....