How do i pass a complex object to another View in ASP.NET MVC?

I'm trying to pass a complex object (that can be serialized, if that helps) to another view.

Currently this is the code i have, in some controller method :-

User user = New User { Name = "Fred, Email = "xxxx" };
return RedirectToAction("Foo", user);

now, i have the following action in the same controller ...

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Foo(User user)
{
 ...
}

When i set a breakpoint in there, the code does stop there, but the value of user is null. What do i need to do? Am i missing something in the global.asax?

cheers :)

Answers


Put your User object in TempData. You can't pass it as a parameter.

TempData["User"]  = new User { Name = "Fred", Email = "xxxx" };
return RedirectToAction("Foo");

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Foo()
{
    User user = (User)TempData["User"];
    ...
}

Similar to How can I maintain ModelState with RedirectToAction?


Need Your Help

Convert host name to IP address then ping

c# properties ip-address

Side note: I really could not think of a good title for this.

Printing PDF file specific pages using vba

vba ms-access pdf printing

Windows allows us to print specific PDF files manually from the printing options. I only need a PDF reader (which can be free) to open the file, then print it by entering specific page numbers. How...