示例#1
0
        // GET: /Student/
        public ViewResult Index(StudentIndexQuery query)
        {
            var model = new StudentIndexResult
            {
                CurrentSort  = query.SortOrder,
                NameSortParm = String.IsNullOrEmpty(query.SortOrder) ? "name_desc" : "",
                DateSortParm = query.SortOrder == "Date" ? "date_desc" : "Date",
            };

            if (query.SearchString != null)
            {
                query.Page = 1;
            }
            else
            {
                query.SearchString = query.CurrentFilter;
            }

            model.CurrentFilter = query.SearchString;
            model.SearchString  = query.SearchString;

            var students = from s in db.Students
                           select s;

            if (!String.IsNullOrEmpty(query.SearchString))
            {
                students = students.Where(s => s.LastName.Contains(query.SearchString) ||
                                          s.FirstMidName.Contains(query.SearchString));
            }
            switch (query.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:     // Name ascending
                students = students.OrderBy(s => s.LastName);
                break;
            }

            int pageSize   = 3;
            int pageNumber = (query.Page ?? 1);

            model.Results = students.ProjectToPagedList <StudentIndexModel>(pageNumber, pageSize);

            return(View(model));
        }
        // GET: /Student/
        public ViewResult Index(StudentIndexQuery query)
        {
            var model = new StudentIndexResult
            {
                CurrentSort = query.SortOrder,
                NameSortParm = String.IsNullOrEmpty(query.SortOrder) ? "name_desc" : "",
                DateSortParm = query.SortOrder == "Date" ? "date_desc" : "Date",
            };

            if (query.SearchString != null)
            {
                query.Page = 1;
            }
            else
            {
                query.SearchString = query.CurrentFilter;
            }

            model.CurrentFilter = query.SearchString;
            model.SearchString = query.SearchString;

            var students = from s in db.Students
                           select s;
            if (!String.IsNullOrEmpty(query.SearchString))
            {
                students = students.Where(s => s.LastName.Contains(query.SearchString)
                                               || s.FirstMidName.Contains(query.SearchString));
            }
            switch (query.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: // Name ascending
                    students = students.OrderBy(s => s.LastName);
                    break;
            }

            int pageSize = 3;
            int pageNumber = (query.Page ?? 1);
            model.Results = students.ProjectToPagedList<StudentIndexModel>(pageNumber, pageSize);

            return View(model);
        }