views:

1820

answers:

4

I hear everyone recommends redirecting the user (HTTP GET) after he submits the form (HTTP POST). It's clean, there is no "do you want to resend" alert, it's simple...

But, what if I want to show some result to the user?

Okay, I can add some parameter to the GET url, like "/?message=1" and then check for that parameter.orm

But, what if I want to show more information? For example, the user submits the form, and the result is a simple structure with let's say... 5 different properties that I want to show to the user.

Something like, "You added a product to the cart, and here are 5 other products that others added as well.". Now, this is simplified, don't tell me "Ah, just pass ?productId=xy and then do another query based on that ID".

Should I maybe stick with POSTBACK model?

Assume the user is anonymous AND without cookies enabled.

+3  A: 

How about something like this:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(string something) {
    if (something == "example") {
        ViewData["Something"] = something;
        ViewData["SOmethingElse"] = 23;
        return View("MyView");
    } else {
        return View("MyView");
    }
}

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index() {
    return View("TheForm");
}

The first method will handle your post event and you can pass data into it and back as needed. The second method will handle the initial get request of the action. Notice the AcceptVerbs attribute on the action.

This option won't change the URL. If you want the url to change your only option is to use a querystring.

Nathan Totten
Explain a bit more please. Don't forget, user doesn't have cookies, and after a POST i'm doing GET to another page.
mitch
If I press F5, will I get the "do you want to resubmit" prompt?
mitch
Yeah, you will. There isn't, to my knowledge, a solution that will solve both of your requirements. You either have to use cookies, use a querystring variable, or allow for possible re-posts.
Nathan Totten
+1  A: 

When the post succeeds, in your controller, persist a transaction identifier for the user (you can use the ASP.NET Profile provider for this) and redirect to a completed page (GET).

In your completed page GET handler you can look up the last transaction ID for the user and display what you want to based on that.

"transaction identifier" is some identifier that you can later reference to look up the details of the transaction.

here is URL sequence I'm thinking of:

  1. GET /orders/create - show form
  2. POST /orders/create - validate and create order (order id == 10), redirect to /orders/details/{orderId}
  3. GET /orders/details/10 - look up order 10, show the order and any extra information related to it
DSO
+3  A: 

That's what TempData is for. It is specifically, and only, for the case where you are redirecting. Use it just like ViewData, except that it will survive a redirect.

Craig Stuntz
I was thinking the same thing and thought that was exactly what he was after. EXCEPT he states it has to work with cookies disabled and IIRC, TempData requires session which in turn requires cookies...?
Charlino
Sessions can also be tracked with query string parameters:http://msdn.microsoft.com/en-us/library/ms178581.aspx
Craig Stuntz
Correct Charlino, it must work with cookies disabled, and TempData relies on Session state which in turn rely on the cookies (except in the cookieless setting, but thats another story).
mitch
Craig - Sure, but with MVC? Anyone?
Charlino
I've not tried MVC+cookie-less-Sessions together, but if it doesn't work it's a bug, IMHO. MVC is intended to support Sessions. (Whether/how much to use them is a different question...)
Craig Stuntz
A: 

You want to use TempData["mykey"]=ObjectIWantToUseOnTheNext Page.

See this blog post for more info:

http://blogs.teamb.com/craigstuntz/2009/01/23/37947/

Mark Perry