10

Hi,

I have a action that looks like this :

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Register(AdRegister adRegister, IEnumerable<HttpPostedFileBase> files)

The AdRegister is a complex class and I need to pass this in to a redirect method further down in the Register action, like this :

return this.RedirectToAction("Validate", adRegister);

The Validate action looks like this :

public ActionResult Validate(AdRegister adRegister)

I do know that I can pass simple parameters but in this case it´s a complex object. This example do not work, the adRegister´s properties will be null.

Is this posible and if so, how?

BestRegards

More Information : Register action will take the adRegister and do som magic on it, then It will be sent to the Validate action. The Validate action will return a validation page to the user. When the user hit the grant button the adRgister will be filled from the form and then sent to the vValidate post where it will be saved. I have looked in to place the adRegister in cache or database temporarily but it will be better if I could simple pass it to the next action.

Banshee
  • 15,376
  • 38
  • 128
  • 219

3 Answers3

18

One possibility would be to pass the simple properties in the query string:

return RedirectToAction(
    "Validate", 
    new { 
        foo = adRegister.Foo, 
        bar = adRegister.Bar, 
        ... and so on for all the properties you want to send
    }
);

Another possibility is to store it in TempData (for the lifetime of the redirect) or Session (for the lifetime of the ASP.NET session):

TempData["adRegister"] = adRegister;
return RedirectToAction("Validate");

and then retrieve it from TempData:

public ActionResult Validate()
{
    adRegister = TempData["adRegister"] as AdRegister;
    ...
}

Yet another possibility (and the one I would recommend you) is to persist this object in the POST method in your datastore:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Register(AdRegister adRegister, IEnumerable<HttpPostedFileBase> files)
{
    ...
    string id = Repository.Save(adRegister);
    return RedirectToAction("Validate", new { id = adRegister.Id });
}

and then fetch it from the data store after you redirect:

public ActionResult Validate(string id)
{
    AdRegister adRegister = Repository.Get(id);
    ...
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Okay, then its not possible to send the complex object direcly to the action. The problem with TempData might be if the user refresh the page. Maby the best way is to store it in database and then run clean up with a set interval(The user could get to stage 2 and then abort, if so, the data will still be in database). – Banshee Mar 23 '11 at 18:44
  • @SnowJim, yes you are correct about the user refreshing the page. That's why I wouldn't recommend you using it. The best way is to store it in the database or Session. – Darin Dimitrov Mar 23 '11 at 18:46
  • @Darin Dimitrov, Yes Session might be the way to go, I will not have to clean the database. The only problem is if the enduser is slow on the submit butten but I supose I could set the session timeout to 60 min. That should do it. This will also take more server memory but it will not involve the database at al. Say that I decide to save it in database, the destination table will be hit alot, Is it better to create a copy of the table and place the temp data there? – Banshee Mar 23 '11 at 18:56
  • @SnowJim, if it is just temporary data the session might make more sense. But if this data could be reused later it would be better to store it permanently in the database. Of course you could also use a temporary table but if you decide to go this route you could probably configure your [session provider to use SQL server](http://idunno.org/articles/277.aspx) so that it doesn't store in memory and it will go automatically to the database. This way you won't be consuming memory on the server and shouldn't care about persistence => it is transparent. – Darin Dimitrov Mar 23 '11 at 19:00
  • @Darin Dimitrov, thanks for all the help! In this case The user will 1. Fill in a form and hit submit 2. The action will get the ad that the user want to post 3. Validation action is triggered and the ad data is sent back to the user 4.a The end user grant the ad and then it is saved in database 4.b goes back to the register form with the ad (auto fill the form with known data). This is the life cycle of the complex object. – Banshee Mar 23 '11 at 19:04
1

an idea would probably create a session variable and pass around a Key that references that session variable if the object is required acorss a few views?

Mark Redman
  • 24,079
  • 20
  • 92
  • 147
  • Yes that could work, the only problem is if the user takes long time to submit the page, session might already be removed. Then it would be better to store it in database. But then again the session time out could be set to 60 min, that should be enough. – Banshee Mar 23 '11 at 18:52
  • If this workflow is something specific, that would take that long and require the session to be held live, keeping the session live via a periodic ajax call would be better than setting the session timeout... – Mark Redman Mar 23 '11 at 18:59
1

ASP.NET MVC's tempdata should be perfect for this.

That said, TempData or Session is one option, but has some downsides like being quite violate and oftentimes murky or difficult to debug. What might be preferable is to "stash" the temporary value in a persistent store, such as the user's profile or your own database, then pass a key through the validate method which can then load the data from said store. This also opens up the possibility of recovering abandoned carts and such.

Wyatt Barnett
  • 15,573
  • 3
  • 34
  • 53
  • aha I do have a cache that i can set timer on. I could store the object in this cache and then every time ti user submits update the timeout to 60 min or somthing like that. This means that I will not have to clean database manually and I can still recover data if the user makes some strange stuff like refreshing and such. – Banshee Mar 23 '11 at 19:00
  • Be careful there -- the ASP.NET cache is more violate than Session storage in most cases. It is the first thing that gets attacked when memory gets short. I would use the Session over the Cache for user data. – Wyatt Barnett Mar 23 '11 at 19:02