第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在 ASP.NET-MVC 中使用 RedirectToAction 傳遞字符串類型列表

在 ASP.NET-MVC 中使用 RedirectToAction 傳遞字符串類型列表

C#
吃雞游戲 2022-07-10 16:37:11
此代碼用于傳遞List<String>withRedirectToAction方法: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();        return RedirectToAction("BrandsOfACategory", new { brands = BrandNames });    }RedirectToAction方法拋出此錯(cuò)誤:無(wú)法將類型“System.Web.Mvc.RedirectToRootResult”隱式轉(zhuǎn)換為“System.Collection.Generic.List”
查看完整描述

2 回答

?
ITMISS

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

}


查看完整回答
反對(duì) 回復(fù) 2022-07-10
?
天涯盡頭無(wú)女友

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”方法中的字符串列表。


查看完整回答
反對(duì) 回復(fù) 2022-07-10
  • 2 回答
  • 0 關(guān)注
  • 117 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)