Passing useful message from a controller to anothe

2019-06-21 19:26发布

问题:

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?

回答1:

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?