hikizume
hikizume

Reputation: 528

Passing multiple objects using RedirectToAction() in mvc?

This works:

 return RedirectToAction("Someplace", object1);

But when I do this:

return View("Someplace", new { object1 = Object1, object2 = Object2 }); 

One of the objects gets dropped.

Already tried wrapping it into a new class and it didn't work, either.

Don't want to use session variables or TempData

Upvotes: 1

Views: 795

Answers (2)

c.dunlap
c.dunlap

Reputation: 600

The best way to do this is to create a new class, referred to as a ViewModel. So Something like this:

public class SomeplaceViewModel()
{
    public object Object1 {get;set;}
    public object Object2 {get;set;}
}

Then in your controller:

var viewModel = new SomeplaceViewModel()
{
     Object1 = object1,
     Object2 = object2
};
return View("Someplace", viewModel);

And finally, in your view you can use the model by inserting this at the top of the view file:

@model SomeplaceViewModel

Upvotes: 1

Kristof
Kristof

Reputation: 3315

You already tried something that looked like this and object2 was null in your view??

var vm = new YourViewModel();
vm.FirstObject = Object1;
vm.TheNextObject = Object2;
return View("Someplace", vm); 

Upvotes: 2

Related Questions