[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 [“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.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。