2 回答

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊
您在操作方法上使用了錯(cuò)誤的返回類型,因?yàn)镽edirectToAction需要返回類型ActionResult而不是List<string>因?yàn)镽edirectToRouteResult繼承自ActionResult.
更新:
您需要將列表序列化為 JSON 字符串才能順利傳遞(帶Newtonsoft.Json庫(kù)),因此目標(biāo)操作方法必須使用string參數(shù)。這是將品牌列表發(fā)送到另一個(gè)操作方法的正確設(shè)置:
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) });
}
目標(biāo)控制器動(dòng)作應(yīng)該是這樣的:
[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貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊
試試下面的代碼,
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 獲取數(shù)據(jù)到“BrandsOfACategory”方法中的字符串列表。
- 2 回答
- 0 關(guān)注
- 117 瀏覽
添加回答
舉報(bào)