3

我有两个模型说“页面”“产品”产品的页面 ID 是指页面的 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 运算符。参数名称:路径

4

1 回答 1

3

代替

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)来检查您的实际情况,但如果不了解您的数据模型,我不能肯定地说。

于 2012-12-13T20:30:55.040 回答