[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 @R_446_4045@ion 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.