我已經(jīng)設(shè)置了一個ViewModel(以前使用ViewBag),但我在以下代碼中遇到了錯誤,我已經(jīng)研究了它,但個人無法找出我的問題: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); }錯誤發(fā)生在該行的底部:viewModel.Products = products;確切的錯誤是“無法將類型隱式轉(zhuǎn)換為 。存在顯式轉(zhuǎn)換(您是否缺少強制轉(zhuǎn)換?)”'System.Linq.IQueryable<OnlineStore.Models.Product>''System.Linq.IQueryable<OnlineStore.ViewModels.ProductIndexViewModel>'我對使用Visual Studio非常陌生,只是想知道我必須做些什么來修復(fù)這個錯誤。
無法隱式轉(zhuǎn)換類型,存在顯式轉(zhuǎn)換(是否缺少強制轉(zhuǎn)換?
慕碼人2483693
2022-08-20 17:35:50