none
Date search RRS feed

  • Question

  • Hi,

    I have view which I created and are trying to do a sort by date in ascending order, it will do only descending datetime order.

    My code is as follows:

    Controller:

     public ActionResult Index(string sortOrder, string currentFilter, string searchString, int? page)
            {
                ViewBag.CurrentSort = sortOrder;
                
                ViewBag.PriceSortParm = sortOrder == "DateCreated" ? "DateCreated_desc" : "Datecreated";

                if (searchString != null)
                {
                    page = 1;
                }
                else
                {
                    searchString = currentFilter;
                }

                ViewBag.CurrentFilter = searchString;

                var items = from i in db.Blogs
                            select i;
                if (!String.IsNullOrEmpty(searchString))
                {
                    items = items.Where(s => s.Title.ToUpper().Contains(searchString.ToUpper())
                                           || s.Title.ToUpper().Contains(searchString.ToUpper()));
                }
                switch (sortOrder)
                {
                    case "Title_desc":
                        items = items.OrderByDescending(s => s.Title);
                        break;
                    case "DateCreated":
                        items = items.OrderBy(s => s.DateCreated);
                        break;
                    case "DateCreated_desc":
                        items = items.OrderByDescending(s => s.DateCreated);
                        break;
                    default:  // Name ascending
                        items = items.OrderBy(s => s.Title);
                        break;
                }

                int pageSize = 3;
                int pageNumber = (page ?? 1);
                return View(items.ToPagedList(pageNumber, pageSize));

    )

    If anyone can help, let me know, it would greatly be appreciated.

    Regards

    Anthony

    Tuesday, March 22, 2016 7:46 PM

Answers