public ActionResult Search(string searchString, string sort) { var model = new ProductIndexViewModel(); using (var db = new EShopContext()) { if (!string.IsNullOrEmpty(searchString)) { var products = db.ProductsTable.Where(p => p.ProductName.Contains(searchString)); foreach (var p in products) { var pModel = new ProductIndexViewModel.ProductListViewModel { ProductId = p.ProductId, ProductName = p.ProductName, ProductDetail = p.ProductDetail, Price = p.ProductPrice, CategoryName = p.Category.CategoryName }; model.Products.Add(pModel); } model = Sort(model, sort); } return(View(model)); } }
public ActionResult ViewAProduct(int?id) { var model = new ProductIndexViewModel.ProductListViewModel(); using (var db = new EShopContext()) { var c = db.ProductsTable.Find(id); model.ProductId = c.ProductId; model.ProductName = c.ProductName; model.ProductDetail = c.ProductDetail; model.Price = c.ProductPrice; model.CategoryName = c.Category.CategoryName; return(View(model)); } }
bool Matches(ProductIndexViewModel.ProductListViewModel product, string SearchProduct, string SearchDescription) { if (!string.IsNullOrEmpty(SearchProduct)) { SearchProduct = SearchProduct.ToLower(); if (!product.Name.ToLower().Contains(SearchProduct)) { return(false); } } if (!string.IsNullOrEmpty(SearchDescription)) { SearchDescription = SearchDescription.ToLower(); if (!product.Description.ToLower().Contains(SearchDescription)) { return(false); } } return(true); }