我已经设置了一个ViewModel(以前使用ViewBag),但我在以下代码中遇到了错误,我已经研究了它,但个人无法找出我的问题:
public ActionResult Index(string category, string search)
{
ProductIndexViewModel viewModel = new ProductIndexViewModel();
var products = db.Products.Include(p => p.Category);
if (!String.IsNullOrEmpty(search))
{
products = products.Where(p => p.Name.Contains(search) ||
p.Description.Contains(search) ||
p.Category.Name.Contains(search));
viewModel.Search = search;
}
viewModel.CatsWithCount = from matchingProducts in products
where matchingProducts.CategoryID != null
group matchingProducts by
matchingProducts.Category.Name into
catGroup
select new CategoryWithCount()
{
CategoryName = catGroup.Key,
ProductCount = catGroup.Count()
};
if (!String.IsNullOrEmpty(category))
{
products = products.Where(p => p.Category.Name == category);
}
viewModel.Products = products;
return View(viewModel);
}
错误发生在该行的底部:
viewModel.Products = products;
确切的错误是
“无法将类型隐式转换为 。存在显式转换(您是否缺少强制转换?)”'System.Linq.IQueryable<OnlineStore.Models.Product>''System.Linq.IQueryable<OnlineStore.ViewModels.ProductIndexViewModel>'
我对使用Visual Studio非常陌生,只是想知道我必须做些什么来修复这个错误。
蛊毒传说
蝴蝶刀刀
相关分类