keep viewdata on RedirectToAction

后端 未结 5 1717
深忆病人
深忆病人 2021-02-01 01:40
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateUser([Bind(Exclude = \"Id\")] User user)
{
        ...
        db.SubmitChanges();
        ViewData[\"info\"] = \         


        
5条回答
  •  名媛妹妹
    2021-02-01 02:21

    You can use TempData.

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

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

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

提交回复
热议问题