2013-03-22 26 views
0

所以我在我的控制器的以下操作:如何返回LINQ查询视图模型

public ActionResult List() { 
     ViewBag.Title = "View Current Stores"; 
     var curStores = 
      (from store in stores.Stores 
      where store.CompanyID == curCompany.ID 
      select store).ToList(); 
     return View(curStores); 
    } 

这是想拿到商店名单,并使其在视图中的表。但是,LINQ语句抛出以下错误:

Non-static method requires a target. 

我该怎么办?

+0

它可能发生,因为'curCompany'为空 – polybios 2013-03-22 17:52:04

+0

是curCompany填充在Controller构造函数中?你可以分享该代码吗? – 2013-03-22 17:57:43

+0

Yup curCompany为空。我是MVC的新手,所以我没有真正弄清楚如何在这个环境下加载数据。 – Bill 2013-03-22 18:08:58

回答

0

尝试使用stores.Stores.Where(o=>o.CompanyID==curCompany.ID).ToList();但要确保在动作中使用但未定义的其他变量(即curCompany和stores)是有效的变量。