2 回答
TA贡献1871条经验 获得超8个赞
您在操作方法上使用了错误的返回类型,因为RedirectToAction需要返回类型ActionResult而不是List<string>因为RedirectToRouteResult继承自ActionResult.
更新:
您需要将列表序列化为 JSON 字符串才能顺利传递(带Newtonsoft.Json库),因此目标操作方法必须使用string参数。这是将品牌列表发送到另一个操作方法的正确设置:
public ActionResult ListOfBrandNames(string id)
{
var result = db.Items.Where(x => x.Category.Name.Equals(id)).Select(x => x.BrandID).ToList();
var ListOfBrands = db.Brands.Where(t => result.Contains(t.BrandID)).ToList();
return RedirectToAction("BrandsOfACategory", new { brands = JsonConvert.SerializeObject(ListOfBrands) });
}
目标控制器动作应该是这样的:
[HttpGet]
public ActionResult BrandsOfACategory(string brands)
{
var listOfBrands = JsonConvert.DeserializeObject<List<Brand>>(brands);
List<string> BrandNames = listOfBrands.Select(f => f.Name.ToString()).ToList();
// do something and return view
}
TA贡献1831条经验 获得超9个赞
试试下面的代码,
public List<String> ListOfBrandNames(string id)
{
var result = db.Items.Where(x => x.Category.Name.Equals(id)).Select(x => x.BrandID).ToList();
var ListOfBrands = db.Brands.Where(t => result.Contains(t.BrandID)).ToList();
List<String> BrandNames = ListOfBrands.Select(f => f.Name.ToString()).ToList();
TempData["Brands"]=BrandNames;
return RedirectToAction("BrandsOfACategory");
}
之后,您可以从 TempData 获取数据到“BrandsOfACategory”方法中的字符串列表。
- 2 回答
- 0 关注
- 91 浏览
添加回答
举报