1

I have a controller that has received a POST back, and has processed what the user has requested. I then build an object, and now, want to RedirectToAction..

return RedirectToAction() ("Index", "Location", r);

Where r is the well named object I am working with. But on the target action, r is null.

public ActionResult Index(LocationByAddressReply location)

Now, I read a few posts on here about this, but am battling to understand.

An option put forward wasL

TempData["myObject"] = myObject;

But that seems ... strange. Not type safe. Is this the most suitable way to pass objects?

Craig
  • 18,074
  • 38
  • 147
  • 248
  • RedirectToAction does exactly that it redirects you. Your alternative might be to use a Session instead. – AliK Apr 05 '13 at 03:43
  • 1
    you can pass an object in your `RedirectToAction("Index", "Location", new { object myObject } )` – Dave Alperovich Apr 05 '13 at 03:49
  • A few questions about this already: http://stackoverflow.com/questions/1352015/redirecttoaction-with-complex-deep-object-fails http://stackoverflow.com/questions/9375279/how-to-pass-class-via-redirecttoaction – Simon C Apr 05 '13 at 04:02
  • Dave A - I tried that, but am still getting null. How would I access this object on the receiving side? – Craig Apr 05 '13 at 04:10
  • 1
    @DaveA Those are `RouteValues`, completely different. Craig: Your only option is to use something that relies on `Session` or persist it to a database.. so `Session` or `TempData` are your only options. – Simon Whitehead Apr 05 '13 at 04:17
  • Thanks Simon... I'll go with TempData... but I need to clear it after I grad the data, right? Or it could get pretty full? – Craig Apr 05 '13 at 04:21
  • ooops, thats true. only primitives can be passed. my bad. – Dave Alperovich Apr 05 '13 at 04:41
  • @DaveA `TempData` is only persisted until you access an item with the same key. So `TempData["blah"] = 12; int i = (int)TempData["blah"];` will remove the data afterwards. – Simon Whitehead Apr 05 '13 at 04:55
  • @SimonWhitehead, I never suggested TempData – Dave Alperovich Apr 05 '13 at 05:35
  • @DaveA Whoops I meant to tag the OP haha. My bad. – Simon Whitehead Apr 05 '13 at 06:37

3 Answers3

4

You can do this in two ways:

First option, if you have a simple model

return RedirectToAction("Index", "Location", new { Id = 5, LocationName = "some place nice" }); 

That one needs maintenance, think about if you need to later on add properties to your model. So you can be fancy and do it like this:

Second option, UrlHelper is your friend

return Redirect(Url.Action("Index", "Location", model));

The second option really is the right way of doing it. model is the object that you built and want to pass to your LocationController.

von v.
  • 16,868
  • 4
  • 60
  • 84
2

Yes you can get values using TempData on redirect. Your method should looks like:

public ActionResult YourRedirectMethod()

{
   TempData["myObject"]=r;
   return RedirectToAction() ("Index", "Location");

}

and

public ActionResult Index()
{
   LocationByAddressReply location=null;
   if(TempData["myObject"]!=null)
    {
          location=(LocationByAddressReply)TempData["myObject"];
    }
}

In this way you get values of your model that was previousely set on redirect method.

Ken Clark
  • 2,500
  • 15
  • 15
  • Thanks. I am implementing this. How do you try ensure the 'TempData' store doesn't get too big? Remove the item after grabbing it? – Craig Apr 05 '13 at 04:28
  • TempData will automatically get empty. Means Temp data use during the current and subsequent request only. This is difference between session and tempdata – Ken Clark Apr 05 '13 at 04:31
  • Check http://stackoverflow.com/questions/313572/what-is-tempdata-collection-used-for-in-asp-net-mvc for more information. – Ken Clark Apr 05 '13 at 04:32
1

I don't think using TempData is the right solution, refer to this answer. You could instead, pass an anonymous object made of your r object. For example, if you have this:

public class UserViewModel 
{
    public int Id { get; set; }
    public string ReturnUrl { get; set; }
}

public ActionResult Index(UserViewModel uvm) 
{ 
    ...
}

you could pass that UserViewModel like this:

public ActionResult YourOtherAction(...)
{
    ...
    return RedirectToAction("Index", "Location", new 
                                                 { 
                                                     id = /*first field*/,
                                                     returnUrl = /*second field*/ 
                                                 });
}

ASP.NET MVC parses this into the object you are expecting as an argument in Index action. Give it a try if you haven't already switched your code for using TempData.

Community
  • 1
  • 1
educampver
  • 2,967
  • 2
  • 23
  • 34