5

I want to make a RedirectToAction after the user clicks a button. Before I redirect, I store the information into a variable. At the end, after I have redirected to action, I want to show some useful information. I tried this:

ViewBag.message = "User with ID = " + id + " was changed status to verified.";

But the data will be flushed after redirection. Is there any other way to achieve this?

gabsferreira
  • 3,089
  • 7
  • 37
  • 61
1myb
  • 3,536
  • 12
  • 53
  • 73

1 Answers1

18

You can use TempData.

TempData["message"] = "User with ID = " + id + " was changed status to verified.";

It is stored in session but after you access it, it will be removed.

Here are some useful links

Passing Data in an ASP.NET MVC Application

Difference Between ViewData and TempData?

Community
  • 1
  • 1
Eranga
  • 32,181
  • 5
  • 97
  • 96
  • Thx, dude =D finally found my solution ^^ i'm new to MVC and finally get it. – 1myb Aug 08 '11 at 06:08
  • one more noob question to ask, may i know why i try to check if this method verify(int id) with if(id != null) else, the code cannot reach else when i type abc.com/verify/ but return me exception The parameters dictionary contains a null entry for parameter 'id', may i know usually how to check what is there value pass in to the method ? Thx – 1myb Aug 08 '11 at 06:47
  • @Spencer its not clear what you are asking. but its pointless to check whether an `int` variable is `null`. – Eranga Aug 08 '11 at 07:18
  • anyway, Thx =D i try to find other approach >.< i mean let say, i got a method called verify, when people visit abc.com/verify/, it will got error, if people visit abc.com/verify/123, it will be fine and execute the function...i want to handle the error by displaying a message, in the way of check if(id != null) else, it return me this message "The parameters dictionary contains a null entry for parameter 'id'" – 1myb Aug 09 '11 at 00:31