加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 保持viewdata在RedirectToAction

发布时间:2020-12-15 18:49:38 所属栏目:asp.Net 来源:网络整理
导读:[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中的“i
[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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读