Exemple #1
0
        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int?page)
        {
            #region Sorting

            // This will keep the current sort order the same while paging
            ViewBag.CurrentSort = sortOrder;

            /*
             * Sorting implementation receives a sortOrder parameter from query string in URL,
             *   does descending on Date by default
             */
            ViewBag.DateSortParm  = string.IsNullOrEmpty(sortOrder) ? "Date"       : string.Empty;
            ViewBag.TitleSortParm = sortOrder == "Title" ? "title_desc" : "Title";

            #endregion

            #region Paging

            /*
             * The first time the page is displayed or if no page /sorting has been clicked, all will be null. If
             *   a page link is clicked, a page variable will contain the current page number to display. This also resets paging
             *   1 if the searchString changes
             */
            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            // Provides the view with the current filter string inbetween changing pages and gets restored to filter box when page gets re-displayed
            ViewBag.CurrentFilter = searchString;

            #endregion

            //// Uses LINQ to Entities and creates an IQueryable variable called blogposts
            ////   Could have dynamically created a LINQ statement instead o writing different ones for each sort order
            IEnumerable <Blogpost> blogposts = _blogpostRepository.GetBlogposts();

            #region Filtering

            // Adding in a conditional where clause if the searchString is not empty

            /*
             * Using if statement incase you change repositories, so if you call an IEnumerable (which is a .NET
             *   Framework implementation) you won't get all rows if the search string is empty and if you call an
             *   IQueryable object (which is a database provider implementation) you still will not get any rows when the search
             *   string is empty. The ToUpper also does the same since the .NET implementation of the Contains method is case-sensitive
             *   while the EF SQL Server providers perform case-insensitive comparisons
             */
            /* Since changing from using the context to the repository, the 'var blogpost' above goes from an
             *   IQueryable to an IEnumerable which returns all blogposts and then filters through them. This can
             *   be inefficient with large amounts of data
             */
            if (!string.IsNullOrEmpty(searchString))
            {
                blogposts = blogposts.Where(b => b.Title.ToUpperInvariant().Contains(searchString.ToUpperInvariant()) ||
                                            b.Subtitle.ToUpperInvariant().Contains(searchString.ToUpperInvariant()) ||
                                            b.User.UserName.ToUpperInvariant().Contains(searchString.ToUpperInvariant()));
            }

            #endregion

            #region Sort Order Switch

            switch (sortOrder)
            {
            case "Date":
                blogposts = blogposts.OrderBy(b => b.CreatedOn);
                break;

            case "Title":
                blogposts = blogposts.OrderBy(b => b.Title);
                break;

            case "title_desc":
                blogposts = blogposts.OrderByDescending(b => b.Title);
                break;

            default:
                blogposts = blogposts.OrderByDescending(b => b.CreatedOn);
                break;
            }

            #endregion

            /*
             *  The ToPagedList extension method acts on the blogposts IQueryable object to convert the query to a single page
             *    of blogposts in a collection type that supports paging. That single page gets passed to the view. The two ?? represent
             *
             */
            const int pageSize   = 3;
            int       pageNumber = (page ?? 1);

            // No database calls are made until converting the IEnumerable object (blogposts) into a collection using ToList()
            return(View("~/Views/Blogpost/Index.cshtml", blogposts.ToPagedList(pageNumber, pageSize)));
        }