How to pass a complex object to another view in ASP.NET MVC?

I am trying to pass a complex object (which can be serialized if that helps) to another view.

This is currently the code that 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 there, the code stops there, but the user value is null . What do I need to do? Am I missing something in global.asax ?

cheers :)

+4
source share
1 answer

Put your User object in TempData. You cannot 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"]; ... } 

How can I support ModelState with RedirectToAction?

+8
source

All Articles