Asp.net mvc 4 具有模型和列表属性的重定向操作
我有一个以模型为帐户的2个视图。从视图一,我使用RedirectToAction转到视图二,并发送模型对象,如下所示:Asp.net mvc 4 具有模型和列表属性的重定向操作,asp.net-mvc-4,Asp.net Mvc 4,我有一个以模型为帐户的2个视图。从视图一,我使用RedirectToAction转到视图二,并发送模型对象,如下所示: [HttpPost] public ActionResult Login(Account account) { //Some code here return RedirectToAction("Index", "AccountDetail", account); } AccountDetail控制
[HttpPost]
public ActionResult Login(Account account)
{
//Some code here
return RedirectToAction("Index", "AccountDetail", account);
}
AccountDetail控制器如下所示:
public ActionResult Index(Account account)
{
return View("ViewNameHere", account);
}
public class Account
{
// Some code here
public List<Details> Details{
get;
set;
}
模型对象包含如下属性:
public ActionResult Index(Account account)
{
return View("ViewNameHere", account);
}
public class Account
{
// Some code here
public List<Details> Details{
get;
set;
}
公共类帐户
{
//这里有一些代码
公开名单详情{
得到;
设置
}
在第一个控制器中,在调用RedirectToAction
之前,有一项详细信息。但是,在第二个控制器的索引方法中,没有任何内容
有人能帮我指出这个漏洞吗?因为我是MVC的初学者,似乎无法理解它。您不应该将复杂的对象传递给GET方法。除了它会创建难看的URL外,您还可以轻松地超过查询字符串限制并抛出异常 在任何情况下,都不能使用
RedirectToAction()
将集合(或包含集合的复杂对象)传递给GET方法。在内部,该方法通过调用.ToString()来使用反射来生成查询字符串
模型每个属性的方法,对于集合属性,该方法类似于。/AccountDetail/Index?Details=System.Collections.Generic.List
调用Index()
方法时,将初始化Account
的新实例,并尝试将其属性Details
的值设置为字符串System.Collections.Generic.List
,但失败,结果是属性Details
为null
选项包括传递标识符并从存储库或
会话
或TempData
获取集合。非常感谢。