Search code examples
asp.net-mvcviewviewdata

ViewData["key"] not available when using RedirectToRoute("Route") in controller


I am working over my first application over MVC3 and still kind of a newbie in it.
Problem is am putting some message in my controller in ViewData dictionary to be shown over the viewpage, but as far as I am using return View("Index"); the meesage is received in view correctly but when I change controller to return through RedirectToRoute("Default").. the ViewData[] dictionary displays null value.

I want to use RedirectToRoute("Default") to change the url address .. Is there any way I can also use ViewData[] with RedirectToRoute("Default")?

public ActionResult Logout()
    {
        Session.Contents.Remove("usrnme");
        Session.Contents.Remove("usrId");
        ViewData["mainMessage"] = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
        //return View("Index");
        return RedirectToRoute("Default");
    }

Solution

  • Try

    TempData["mainMessage"] = ....
    

    and in Default

    if (TempData["mainMessage"] != null) 
    {    
        ViewData["mainMessage"] = TempData["mainMessage"];
    }