5.1 按照价格对产品进行排序
为了演示如何进行排序,我们将使用一个简单的例子让用户可以对产品按照价格进行排序。
首先,我们向Controllers\ProductsController.cs文件中的Index方法添加一个switch语句,以便可以按照价格对产品信息进行排序,修改之处如下列高亮显示的代码:
1 public ActionResult Index(string category, string search, string sortBy) 2 { 3 // instantiate a new view model 4 ProductIndexViewModel viewModel = new ProductIndexViewModel(); 5 6 // select the products 7 var products = db.Products.Include(p => p.Category); 8 9 // perform the search and save the search string to the vieModel10 if (!string.IsNullOrEmpty(search))11 {12 products = products.Where(p => p.Name.Contains(search) || p.Description.Contains(search) || p.Category.Name.Contains(search));13


