tags:

views:

125

answers:

1

I have a controller that handles three actions that are specific to my problem.

The first is the edit action which returns a view with an HTML form that the user can edit properties on the given item.

The second is the update action which accepts the post back form the browser and updates the database. When the update is successful we do a redirect to action.

The third is the show action which shows the details of the given item. This action is where we get redirected to after a successful update.

The flow is:

Show -> Edit -> Update (Sucess: y -> redirect to Show, n -> return Edit)

What I want to achieve is to have a flag tripped when the update was successful so that on the next Show view I can display a confirmation message for the user. The problem is that I'm not 100% sure on the best way to carry that data over the RedirectToAction() call. One thought I had was using a query string? We are already carrying variables around with the query string for another purpose but part of my is skeptical to abuse that. The call to the redirect is below.

RouteValueDictionary dict = Foo.GetRouteValues(bar);

RedirectToAction("Show", dict);

I've read this question as well but am leary about using the TempData property if I don't have to.

Question

Thanks for some suggestions!

+2  A: 

EDIT: Sorry, didn't originally see your note about not wanting to use TempData.

In a nutshell - do you want your message to reappear if the client refreshes/reloads the page they've been redirected to?

If you do, then use the querystring, something like:

return(RedirectToAction("Index", new { message = "hi there!" }));

and then either define

public ActionResult Index(string message) { }

or explicitly pull out Request.QueryString["message"] and pass it to the View via ViewData in the usual way. This will also work on browsers that aren't accepting cookies from your site.

If you DON'T want the message to display again, then ASP.NET MVC 1.0 provides the TempData collection for this exact purpose.

TempData property values are stored in session state until the next request from the same browser, after which they are cleared - so if you put something in TempData immediately before returning RedirectToAction, it'll be available on the result of the redirect but will be cleared immediately afterwards.

Here's a simple change to the HomeController in the ASP.NET MVC startup project:

public ActionResult Index() {
    ViewData["Message"] = "Welcome to ASP.NET MVC!";
    return View();
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(string submitButton) {
    TempData["message"] = "You clicked " + submitButton;
return(RedirectToAction("Index"));
}

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

and the corresponding view /Views/Home/Index.aspx should contain something like this:

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
  <% if (TempData["message"] != null) { %>
    <p><%= Html.Encode(TempData["message"]) %></p>
  <% } %>
  <% using (Html.BeginForm()) { %>
    <input type="submit" name="submitButton" value="Button One" />
    <input type="submit" name="submitButton" value="Button Two" />
  <% } %>
</asp:Content>

You'll notice the TempData message is displayed immediately following a POST-REDIRECT-GET sequence, but if you refresh the page, it won't be displayed again.

Note that this behaviour has changed in ASP.NET MVC 2 - see "Passing State between Action Methods" in this article for more information.

Dylan Beattie
Thanks for the nice explanation. I'm going to take a look at the article since we are on 2.0. I mentioned in my post that I wanted to stay away from the TempData property however if that is the defacto way then maybe it won't be so bad. Thanks again.
Mike