我正在将模型项传递到Visual Studio 2015中的字典错误中。错误消息显示:onCreateView
1 [CmsShoppingCart.Models.Data.ProductDTO]',但是此字典需要一个模型项类型“ System.Collections.Generic.IEnumerable`1 [CmsShoppingCart.Models.ViewModels.Shop.ProductVM]”。
在Visual Studio中,我在返回View(productVMList)时收到“检测不到的代码”; 我要实现的是查看产品列表和有效的产品搜索栏。 我的控制器是
The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery
视图是
public ActionResult Category(string name,string searchString)
{
// Declare a list of ProductVM
List<ProductVM> productVMList;
using (Db db = new Db())
{
// Get category id
CategoryDTO categoryDTO = db.Categories.Where(x => x.Slug == name).FirstOrDefault();
int catId = categoryDTO.Id;
// Init the list
productVMList = db.Products.ToArray().Where(x => x.CategoryId == catId).Select(x => new ProductVM(x)).ToList();
// Get category name
var productCat = db.Products.Where(x => x.CategoryId == catId).FirstOrDefault();
ViewBag.CategoryName = productCat.CategoryName;
}
var product = from c in db.Products
select c;
if (!string.IsNullOrEmpty(searchString))
{
product = product.Where(c => c.Name == searchString);
}
return View(product);
// Return view with list
return View(productVMList);
}