【问题标题】:Include path expression must refer to a navigation property defined on the type包含路径表达式必须引用在类型上定义的导航属性
【发布时间】:2012-12-13 06:13:22
【问题描述】:

我有两个模型说 "Page""Product" Product's PageID 指的是 Page's ID

在我的产品索引视图中,我需要将页面列表作为下拉列表,因为我正在使用

public ViewResult Index()
{
   var products = _db.Products.Include(p => p.Page);
            return View(products.ToList());
}

但我只需要那些 PageGroup 属性值为 'Product' 的页面。为此我使用了

public ViewResult Index()
{
    var products = _db.Products.Include(p => p.Page.PageGroup
                                   .Contains(PageGroup.Product.ToString()));
    return View(products.ToList());
} 

报错如下:

包含路径表达式必须引用导航属性 在类型上定义。使用虚线路径进行参考导航 属性和用于集合导航的 Select 运算符 特性。参数名称:路径

【问题讨论】:

    标签: asp.net-mvc-3 collections model path


    【解决方案1】:

    而不是

    var products = _db.Products.Include(p => p.Page.PageGroup.Contains(PageGroup.Product.ToString()));

    你想要这样的东西。

    var products = _db.Products.Include(p => p.Page).Where(p => p.Page.PageGroup.Contains(PageGroup.Product.ToString());

    您可能必须包含更多子属性(如 PageGroup)来检查您的实际情况,但如果不了解您的数据模型,我无法确定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 2016-02-26
      • 1970-01-01
      相关资源
      最近更新 更多