Session State is erased after calling RedirectToAction() in ASP.NET MVC 3

微笑、不失礼 提交于 2019-12-11 05:58:32

问题


I use a simple sequences:

  1. Set a Session State in [HttpGet] method.
  2. Redirect to another action using RedirectToAction() in [HttpPost] method.
  3. Want to get the value of that Session State, in the destination.

Problem:

If user hits "submit" button on my "Table" view, all the data inside session got cleared and I can't get them in the destination action (which is "Table"). Here is the code:

   [HttpGet]
    public ActionResult Edit(string TableName, int RowID, NavigationControl nav)
    {
        if (nav != null) Session["NavigationData"] = nav;

        myService svc = new myService (_repository);
        EditViewModel model = new EditViewModel();

        model.TableDefinitions = svc.GetTableDefinition(TableName);
        model.RowData = svc.GetRowData(model.TableDefinitions.Name, RowID);

        return View(model);
    }

    [HttpPost]
    public ActionResult Edit(EditViewModel model)
    {
        MyService svc = new MyService (_repository);
        svc.SaveRowData(model.TableDefinitions.Name, model.RowData);
        return RedirectToAction("Table");
    }

    public ActionResult Table(string TableName)
    {
        myService svc = new myService (_repository);

        TableViewModel model = new TableViewModel();
        model.TableDefinition = svc.GetTableDefinition(TableName);

        NavigationControl nav = (NavigationControl)Session["NavigationData"];
        if (nav != null)
        {
            model.NavigationControl = nav;
        }

        return View(model);
    }

and Session["NavigationData"] is always null when user reaches it via: return RedirectToAction("Table"). If user hits an HTML link on "Edit" View, Session["NavigationData"] can restore its value in "Table" method!

Any idea about what's going on? Who deletes the Session state?!


回答1:


My browser cookie was off but state was not set to cookie-less.



来源:https://stackoverflow.com/questions/7616340/session-state-is-erased-after-calling-redirecttoaction-in-asp-net-mvc-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!