Session State is erased after calling RedirectToAc

2019-08-07 03:24发布

问题:

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.