【问题标题】:Passing List of String type with RedirectToAction in ASP.NET-MVC在 ASP.NET-MVC 中使用 RedirectToAction 传递字符串类型列表
【发布时间】:2019-06-01 21:48:02
【问题描述】:

此代码用于通过RedirectToAction 方法传递List<String>

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 方法抛出此错误:

无法将类型“System.Web.Mvc.RedirectToRootResult”隐式转换为“System.Collection.Generic.List”

【问题讨论】:

  • 在您提供的列表中给出了 2 个答案。两者相互确认,所以我实施并检查但在浏览器中出现此错误:Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /Home/ListOfBrandNames Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3282.0。请删除重复项

标签: c# asp.net-mvc


【解决方案1】:

试试下面的代码,

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】:

    您在操作方法上使用了错误的返回类型,因为RedirectToAction 需要ActionResult 而不是List&lt;string&gt; 的返回类型,因为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
    }
    

    参考:

    How to pass List in Redirecttoaction

    【讨论】:

    • 抛出异常:Message: "Self referencing loop detected for property 'Brand' with type 'System.Data.Entity.DynamicProxies.Brand_6F41EA48F10B0AC41C001572E51DD1493029140C74DC2BC1D4D6FFE059AB59EC'. Path '[0].Items[0]'." string 在您上面给出的第一个操作方法代码中的return RedirectToAction("BrandsOfACategory", new { brands = JsonConvert.SerializeObject(ListOfBrands) }); 行。
    猜你喜欢
    • 2012-02-25
    • 2012-08-24
    • 2011-09-06
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 2018-12-17
    • 1970-01-01
    相关资源
    最近更新 更多