Have you ever wondered how to display a temporary message after redirecting to a new page in ASP.NET MVC? In this post, I will show you a simple way of displaying a success message after calling RedirectToAction.
Why you can’t use the ViewData dictionary?
When using the Post Redirect Get (PRG) pattern, you will find that data stored in the ViewData
dictionary is lost after the call to RedirectToAction
. This problem can be solved by using the TempData
dictionary instead of the ViewData
dictionary.
What is the TempData dictionary?
Unlike the ViewData
dictionary, data stored in the TempData
dictionary will be kept for the next request. This means you can set your message in the POST request and display the message after the redirect to the GET request.
An example that displays a success message
The following example assumes you have created a new ASP.NET MVC application using the MVC template from Visual Studio 2013 with no authentication.
1. Create a message controller
Create a new controller called MessageController
and then add the following code:
public class MessageController : Controller
{
[ChildActionOnly]
public ActionResult TempMessage()
{
return PartialView();
}
}
Notice how the ChildActionOnly
attribute has been applied to the action. This is because we will be using the Html.Action
helper to render this child action as a partial view. There’s other ways of displaying the message, but I find it’s best to do it this way so that donut caching can be used in the future.
2. Create a view to display the message
Create a new view named TempMessage
and add the following code to it:
@{
string message = TempData["Message"] as string;
if (!String.IsNullOrEmpty(message))
{
<div class="message">
<p>@message</p>
</div>
}
}
There’s not much going on here, it’s a partial view that will be returned using the PartialView
method in the TempMessage
method.
3. Add the call to render the TempMessage action
Open your Layout file and add the following line of code where you want the message displayed:
@Html.Action(actionName: "TempMessage", controllerName: "Message")
Sometimes you might wan’t to cache the page but still display temporary messages. A good way to do this, is to create your own @Html.Action
method that implements donut caching. That’s why I find it best to use a child action to render the temporary message.
4. Modify the HomeController to test the action out
Modify the HomeController
so that it looks like the following code:
public class HomeController : Controller
{
public ActionResult Index()
{
TempData["Message"] = "Update Success";
return RedirectToAction("About");
}
public ActionResult About()
{
return View();
}
}
Now if you run the application, you will be redirected to the About page and the temporary message will be displayed. When you refresh the About page, the message will disappear.
Final thoughts
This post has shown a very basic example of displaying a message after a redirect. You could expand on the example to display different types of messages like errors, notices, warnings. Another thing that’s worth doing is creating a base controller or extension methods that provide a strongly typed way of setting the messages.