public static MvcHtmlString PageLinks(this HtmlHelper html, PagingDisplay pagingInfo, Func<int, string> pageUrl)
 {
     StringBuilder result = new StringBuilder();
     for (int i = 1; i <= pagingInfo.TotalPage; i++)
     {
         TagBuilder tag = new TagBuilder("a");
         tag.MergeAttribute("href", pageUrl(i));
         tag.MergeAttribute("style","padding:5px");
         tag.InnerHtml = i.ToString();
         if (i == pagingInfo.CurrentPage)
         {
             tag.AddCssClass("selected");
         }
         result.Append(tag.ToString());
     }
     return MvcHtmlString.Create(result.ToString());
 }
        public ActionResult CateFilter(int id, int page = 1)
        {
            bool blnValidCate = false;
            string strCateName = "";
            PagingDisplay pagingView = new PagingDisplay();
            using (var DbContext = new CocBookEntities())
            {
                // list Category
                pagingView.CateList = (from c in DbContext.Categories
                                       where c.Active == true
                                       orderby c.Position
                                       select c).ToList();
                // know Category
                foreach (var item in pagingView.CateList)
                {
                    if (item.CateID == id)
                    {
                        blnValidCate = true;
                        strCateName = item.Name;
                    }
                }

                // if invalid URL
                if (!blnValidCate)
                {
                    return RedirectToAction("Index", "Home");
                }
                // load books
                var books = (from b in DbContext.V_Book
                             join c in DbContext.BookInCategories on b.BookID equals c.BookID
                             where c.CateID == id
                             orderby b.CreatedDate descending
                             select b).ToList();

                pagingView.TotalItem = books.Count;
                pagingView.ItemsPerPage = PageSize;
                pagingView.CurrentPage = page;
                pagingView.TotalPage = (int)Math.Ceiling((decimal)pagingView.TotalItem / pagingView.ItemsPerPage);
                pagingView.BookList = books.Skip((page - 1) * PageSize).Take(PageSize).ToList();
            }
            ViewBag.SelectedID = id;
            ViewBag.Category = strCateName;

            ViewBag.PageCategory = id + "/" + ChangeToUrlString(strCateName);
            return View(pagingView);
        }
        public ActionResult NewBooks(int page=1)
        {
            PagingDisplay pagingView = new PagingDisplay();
            using (var DbContext = new CocBookEntities())
            {
                pagingView.CateList = (from c in DbContext.Categories
                                          where c.Active == true
                                          orderby c.Position
                                          select c).ToList();
                var books = (from c in DbContext.V_Book
                             where c.Active == true
                             orderby c.CreatedDate descending
                             select c).ToList();

                pagingView.TotalItem = books.Count;
                pagingView.ItemsPerPage = PageSize;
                pagingView.CurrentPage = page;
                pagingView.TotalPage = (int)Math.Ceiling((decimal) pagingView.TotalItem/ pagingView.ItemsPerPage);
                pagingView.BookList = books.Skip((page - 1) * PageSize).Take(PageSize).ToList();
            }
            return View(pagingView);
        }
        public ActionResult Search(string str, int type = 1, int page = 1)
        {
            PagingDisplay pagingView = new PagingDisplay();
            using (var DbContext = new CocBookEntities())
            {
                pagingView.CateList = (from c in DbContext.Categories
                                       where c.Active == true
                                       orderby c.Position
                                       select c).ToList();
                List<V_Book> books = null;

                if (type == 1)
                {
                    books = (from c in DbContext.V_Book
                             where c.Active == true && c.Name.Contains(str)
                             orderby c.CreatedDate descending
                             select c).ToList();

                }
                else
                {
                    books = (from c in DbContext.V_Book
                             where c.Active == true && c.AuthorName.Contains(str)
                             orderby c.CreatedDate descending
                             select c).ToList();
                }
                pagingView.TotalItem = books.Count;
                pagingView.ItemsPerPage = PageSize;
                pagingView.CurrentPage = page;
                pagingView.TotalPage = (int)Math.Ceiling((decimal)pagingView.TotalItem / pagingView.ItemsPerPage);
                pagingView.BookList = books.Skip((page - 1) * PageSize).Take(PageSize).ToList();
                ViewBag.SearchKey = str;
                ViewBag.SearchType = type + "";
                ViewBag.eid = 0;
            }
            return View(pagingView);
        }
        public ActionResult Search(FormCollection form)
        {
            int page = 1;
            string str = form["searchkey"];
            if (str.Trim() == "")
            {
                return RedirectToAction("Index");
            }
            string type = form["searchtype"] == "" ? "1" : form["searchtype"];
            PagingDisplay pagingView = new PagingDisplay();
            using (var DbContext = new CocBookEntities())
            {
                SearchHistory history = new SearchHistory();
                history.SearchValue = str + ";"+type ;
                history.Username = getUser();

                pagingView.CateList = (from c in DbContext.Categories
                                       where c.Active == true
                                       orderby c.Position
                                       select c).ToList();
                List<V_Book> books = null;

                if (type.Equals("1"))
                {
                    books = (from c in DbContext.V_Book
                             where c.Active == true && c.Name.Contains(str)
                             orderby c.CreatedDate descending
                             select c).ToList();
                }
                else
                {
                    books = (from c in DbContext.V_Book
                             where c.Active == true && c.AuthorName.Contains(str)
                             orderby c.CreatedDate descending
                             select c).ToList();

                }
                // store history of search
                history.HitCount = books.Count;
                history.CreatedDate = DateTime.Now;
                DbContext.SearchHistories.Add(history);
                DbContext.SaveChanges();

                // data mining search history
                string strS = str + ";" + type ;
                var extend = (from c in DbContext.SearchHistories
                              where c.SearchValue.Contains(str) && c.SearchValue.Contains(type) &&c.SearchValue!= strS && c.HitCount < books.Count
                              orderby c.HitCount descending
                              select c).Take(1).SingleOrDefault();
                int eid = 0;
                string extendSearch="";
                string extendType = "";
                if (extend != null)
                {
                    eid = extend.AutoID;
                    extendSearch = extend.SearchValue.ToString().Split(';')[0];
                    extendType = extend.SearchValue.ToString().Split(';')[1];
                }

                // display
                pagingView.TotalItem = books.Count;
                pagingView.ItemsPerPage = PageSize;
                pagingView.CurrentPage = page;
                pagingView.TotalPage = (int)Math.Ceiling((decimal)pagingView.TotalItem / pagingView.ItemsPerPage);
                pagingView.BookList = books.Skip((page - 1) * PageSize).Take(PageSize).ToList();
                ViewBag.SearchKey = str;
                ViewBag.SearchType = type;
                ViewBag.ExtendSearch = extendSearch;
                ViewBag.ExtendType = extendType;
                ViewBag.eid = eid;
            }
            return View(pagingView);
        }