我如何通过一个复杂的对象,在ASP.NET MVC另一个View? [英] 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.
目前,这是code,我有,在某些控制器方法: -
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)
{
...
}
当我设置有断点时,code没有止步,而是价值用户
是空
。
我需要做什么?我失去的东西的的Global.asax
?
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
?
欢呼:)
推荐答案
把你的用户对象的TempData。你不能把它作为一个参数传递。
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"];
...
}
类似<一个href=\"http://stackoverflow.com/questions/279665/how-can-i-maintain-modelstate-with-redirecttoaction#279740\">http://stackoverflow.com/questions/279665/how-can-i-maintain-modelstate-with-redirecttoaction#279740
这篇关于我如何通过一个复杂的对象,在ASP.NET MVC另一个View?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!