1

In my controller say ControllerFirst I am setting a ViewBag property by the below line.

ViewBag.PreviousPage="ThisIsComingFromControllerFirst";
return RedirectToAction("ControllerSecond", "Home");

Now from here:

public ActionResult ControllerSecond()
{
    return View();
}

I am trying to use Viewbag in the ControllerSecond by the following

View: ControllerSecond.cshtml

@if(ViewBag.PreviouPage == "SomeValue")
{
  //do this
}

But ViewBag.PreviousPage value is null.

Please let me know why its null, what could I do to get the value in my view from the ControllerFirst.

I have done this one using Session, but We don't want to sessions..

Any other options?

Paolo Moretti
  • 54,162
  • 23
  • 101
  • 92
Hari Gillala
  • 11,736
  • 18
  • 70
  • 117
  • Use TempData, see [http://stackoverflow.com/questions/5753720/passing-info-to-another-action-using-redirecttoaction-mvc][1] [1]: http://stackoverflow.com/questions/5753720/passing-info-to-another-action-using-redirecttoaction-mvc – wnascimento Nov 03 '11 at 10:10
  • 1
    As I can see you left 's' in PrevousPage in your code: @if(ViewBag.PreviouPage == "SomeValue"), so please clarify that you've copied it from your view or you typed it? – Amir978 Nov 03 '11 at 11:22

4 Answers4

3

To answer your first question, ViewBag is a more convenient form of ViewData that uses dynamic objects rather than generic ones. As with ViewData, ViewBag only exists for the life of a single request, thus it is only available between the ActionMethod and it's view. It is not available between different ActionMethods.

Think of it like an intercom system in a home. You can send messages to other parts of the home, but you can't send a message to a neighbors home.

The only other options you have are:

  • Use Session
  • Use TempData (which also uses session)
  • Use a Cookie
  • Use a querystring parameter
  • Use an intermediate table in your database
  • Post to ActionMethod
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
1

ViewBag (and ViewData) are objects for accessing extra data (i.e., outside the data model), between the controller and view.

If your data have to persist between two subsequent requests you can use TempData.

However, TempData is by default stored in the session. So, if you don't want to use sessions, you could use cookies and somehow duplicate a bit of what session does for you, as MikeSW suggested.

When to use ViewBag, ViewData, or TempData in ASP.NET MVC 3 applications

Community
  • 1
  • 1
Paolo Moretti
  • 54,162
  • 23
  • 101
  • 92
  • 1
    Its also possible to change the default storage of `TempData`, but as Paolo states, the purpose of TempData is to store data until the next subsequent read, not indefinitely, so sounds kind of like what you want. – Paul Tyng Nov 03 '11 at 11:31
0

Use TempData instead of ViewBag

wnascimento
  • 1,949
  • 1
  • 19
  • 16
0

You can use a cookie and somehow duplicate a bit of what session does for you.

MikeSW
  • 16,140
  • 3
  • 39
  • 53