https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application
public ActionResult Index(string sortOrder){
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
var students = from s in db.Students
select s;
switch (sortOrder)
{
case "name_desc":
students = students.OrderByDescending(s => s.LastName);
break;
case "Date":
students = students.OrderBy(s => s.EnrollmentDate);
break;
case "date_desc":
students = students.OrderByDescending(s => s.EnrollmentDate);
break;
default:
students = students.OrderBy(s => s.LastName);
break;
}
return View(students.ToList());
}
三元运算符仅根据条件返回结果之一,它们不会更改 sortOrder 对吗?
如果 sortOrder 是“Date”,那么代码将设置ViewBag.DateSortParm 为“date_desc”,但 sortOrder 不是仍然是“Date”,所以它实际上不会改变顺序?
我看不出这段代码如何正常工作,但确实如此。
HUX布斯
相关分类