-1

I have the following tempdata in my controller:

    public ActionResult Index(string query = null)
    {

        TempData["message"] = string.Format("test message");

        return RedirectToAction("Index", "Posts");
    }

And in my _layout.cshtml file I have the following:

 @if (TempData["message"] != null)
 {
      <div class="message">@TempData["message"]</div>
 }

But nothing get displayed. If do the following i.e. no redirect from my controller method, the message gets displayed.

public ActionResult Index(string query = null)
{

        TempData["message"] = string.Format("test message");

         return View();

}

So basically it doesnt seem to work on RedirectToAction. Strangely it was working before so not sure what has happend all of a sudden?

Any ideas?

adam78
  • 9,668
  • 24
  • 96
  • 207

1 Answers1

0

You have to serialize the object before assigning it to TempData. For example,

AccountHolderDetails user = new AccoundHolderDetails();
TempData["UserData"] = JsonConvert.SerializeObject(user);

and retrieve the object by deserializing it.

var user = JsonConvert.DeserializeObject<AccountHolderDetails>(TempData["UserData"].ToString());