vyclarks
vyclarks

Reputation: 878

Session Variable is null after using RedirectToAction

After editing information in view Edit.cshtml, I set a session variable Session["ToastMessage"] = "Sucessfully"; to show it in Edit.cshtml after that is loaded again. But I got Session["ToastMessage"] = null in View.

        // GET: About/Edit/
        public ActionResult Edit()
        {
            AboutInformation about = LoadDataFromConfigFile();
            return View(about);
        }

        // POST: About/Edit/
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "NameApp,ContactInformation,Email,Fax,Phone,ReleaseDay,LogoPathOfProduction,IsShow,LatestVersion,ReleaseNotes")] AboutInformation about,List<string> imagesToDelete, List<HttpPostedFileBase> images, bool? isShowInfo)
        {
            if (ModelState.IsValid)
            {
                //update....
            }

            Session["ToastMessage"] = "Sucessfully";
            return RedirectToAction("Edit");
        }

Tried to debug, it showed Session["ToastMessage"] = null when it is just passed to Get method:

  // GET: About/Edit/
            public ActionResult Edit()
            {
            }

Session is clear somehow, have been using Session a lots in my app, and it worked fine. Does ASP just clear session at any random time? Need help!

Upvotes: 2

Views: 1648

Answers (4)

Ahsan Siddiqui
Ahsan Siddiqui

Reputation: 11

Check for your localhost browser cookies or any server you are using; they should not be blocked. Allow cookies.

Upvotes: 0

Sid
Sid

Reputation: 2582

You can use @hien-nguyen answer to directly return to view and show the toast message. However in your case you are redirecting to another action method. So, You can use Keep method of Temp data like this:

// POST: About/Edit/
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "NameApp,ContactInformation,Email,Fax,Phone,ReleaseDay,LogoPathOfProduction,IsShow,LatestVersion,ReleaseNotes")] AboutInformation about,List<string> imagesToDelete, List<HttpPostedFileBase> images, bool? isShowInfo)
    {
        if (ModelState.IsValid)
        {
            //update....
        }

        TempData["ToastMessage"] = "Sucessfully";
        var message = TempData["ToastMessage"];
        TempData.Keep("ToastMessage");
        return RedirectToAction("Edit");
    }

And in Get Method:

public ActionResult Edit()
        {
            AboutInformation about = LoadDataFromConfigFile();
            var message = TempData["ToastMessage"];
            return View(about);
        }

Upvotes: 0

Ranjith.V
Ranjith.V

Reputation: 326

You can use TempData while redirect to one ActionMethod to Another Action.

var Status=TempData["ToastMessage"];

Upvotes: 0

Hien Nguyen
Hien Nguyen

Reputation: 18975

With your case, you can use TempData instead of Session.

TempData["ToastMessage"] = "Sucessfully";

TempData can be used to store temporary data which can be used in the subsequent request.

public ActionResult Edit()
        {
            var result = TempData["ToastMessage"];
            // check result here
            AboutInformation about = LoadDataFromConfigFile();
            return View(about);
        }

Upvotes: 1

Related Questions