嗨我无法获取产品的详细信息,代码Sneaker snkr=_sneaker_categoryDataModel.sneakers.Find(id);
似乎无效,因为.Find
'不包含查找的定义'
public class HomeController : Controller
{
//
// GET: /Home/
private Forest.Models.ForestDBEntities
_music_categoryDataModel = new Forest.Models.ForestDBEntities();
//private Models.music_categories music_categoryToCreate;
public ActionResult Index()
{
IList<Forest.Models.music_categories> Categories = _music_categoryDataModel.music_categories.ToList<Forest.Models.music_categories>();
return View("Categories",Categories);
}
public ActionResult Details (int id)
{
Sneaker snkr=_sneaker_categoryDataModel.sneakers.Find(id);
return View(snkr);
}
}
答案 0 :(得分:2)
_sneaker_categoryDataModel.sneakers
最有可能是IQueryable<> / IEnumerable<>,它没有名为Find
的方法。 Find是List<T>
上的一种方法,因此您可以使用此方法的唯一方法是在ToList()
之前调用Find
(这可能会导致负面的性能影响,具体取决于集合)。
有各种LINQ扩展使查询这些类型的集合变得微不足道,SingleOrDefault或FirstOrDefault将是一个适当的选择
Sneaker snkr = _sneaker_categoryDataModel.sneakers.FirstOrDefault(x => x.Id == id);