C# 如何在asp.net mvc中将数组发送到另一个控制器方法?

C# 如何在asp.net mvc中将数组发送到另一个控制器方法?,c#,asp.net-mvc,C#,Asp.net Mvc,客户是一个列表 当我发送列表时,它包含4个项目,但当我在控制器方法中接收到它时,它只有一个项目,类型为generic list。那似乎不是我想要的。但如何在控制器方法之间传递比字符串和整数更复杂的数据呢 重定向时无法发送复杂对象。重定向时,您正在向目标操作发送GET请求。发送GET请求时,需要将所有信息作为查询字符串参数发送。这只适用于简单的标量属性 因此,一种方法是在重定向(例如在数据库中)之前将实例保存在服务器上的某个位置,然后仅将id作为查询字符串参数传递给目标操作,该操作将能够从存储对象

客户是一个
列表


当我发送列表时,它包含4个项目,但当我在控制器方法中接收到它时,它只有一个项目,类型为generic list。那似乎不是我想要的。但如何在控制器方法之间传递比字符串和整数更复杂的数据呢

重定向时无法发送复杂对象。重定向时,您正在向目标操作发送GET请求。发送GET请求时,需要将所有信息作为查询字符串参数发送。这只适用于简单的标量属性

因此,一种方法是在重定向(例如在数据库中)之前将实例保存在服务器上的某个位置,然后仅将id作为查询字符串参数传递给目标操作,该操作将能够从存储对象的位置检索该对象:

int id = Persist(customers);
return RedirectToAction("ListCustomers", new { id = id });
在目标行动中:

public ActionResult ListCustomers(int id)
{
    IEnumerable<string> customers = Retrieve(id);
    ...
}
然后:

public ActionResult ListCustomers()
{
     TempData["customers"] as IEnumerable<string>;
    ...
}
公共操作结果列表客户()
{
TempData[“客户”]作为IEnumerable;
...
}
public ActionResult Index()
{
    IEnumerable<string> customers = new[] { "cust1", "cust2" };
    var values = new RouteValueDictionary(
        customers
            .Select((customer, index) => new { customer, index })
            .ToDictionary(
                key => string.Format("[{0}]", key.index),
                value => (object)value.customer
            )
    );
    return RedirectToAction("ListCustomers", values);
}

public ActionResult ListCustomers(IEnumerable<string> customers)
{
    ...
}
TempData["customer"] = customers;
return RedirectToAction("ListCustomers");
public ActionResult ListCustomers()
{
     TempData["customers"] as IEnumerable<string>;
    ...
}