public ActionResult Index(ShoppingCartModels inputModel) { ShoppingCartModels model = services.GetShoppingCartModel(inputModel, Request.Cookies, null); // set cookies (150914) HttpCookie cookieSortBy = new HttpCookie("cookieSortBy"); cookieSortBy.Value = model.currentSortBy.ToString(); Response.Cookies.Add(cookieSortBy); HttpCookie cookieSortOrder = new HttpCookie("cookieSortOrder"); cookieSortOrder.Value = model.currentSortOrder.ToString(); Response.Cookies.Add(cookieSortOrder); HttpCookie cookieBeginWith = new HttpCookie("cookieBeginWith"); cookieBeginWith.Value = model.currentBeginWith.ToString(); Response.Cookies.Add(cookieBeginWith); HttpCookie cookiePage = new HttpCookie("cookiePage"); cookiePage.Value = model.currentPage.ToString(); Response.Cookies.Add(cookiePage); HttpCookie cookiePageSize = new HttpCookie("cookiePageSize"); cookiePageSize.Value = model.currentPageSize.ToString(); Response.Cookies.Add(cookiePageSize); return View(model); //if (model.isSubmit != "") return PartialView("ProductList", model); //else return View(model); }
/// <summary> /// if productID is not null, will force showing this record by adjusting current page (150923) /// </summary> public ShoppingCartModels GetShoppingCartModel(ShoppingCartModels inputModel, HttpCookieCollection cookies, string productID) { ShoppingCartModels n = new ShoppingCartModels(); if (inputModel == null) { inputModel = new ShoppingCartModels(); inputModel.newBeginWith = "all"; inputModel.newSortOrder = "ascending"; inputModel.newSortBy = "productID"; inputModel.isSubmit = ""; inputModel.newPage = 1; inputModel.newPageSize = 10; if (cookies != null) { if (cookies["cookieSortBy"] != null) inputModel.newSortBy = cookies["cookieSortBy"].Value; if (cookies["cookieSortOrder"] != null) inputModel.newSortOrder = cookies["cookieSortOrder"].Value; if (cookies["cookieBeginWith"] != null) inputModel.newBeginWith = cookies["cookieBeginWith"].Value; if (cookies["cookiePage"] != null) inputModel.newPage = int.Parse(cookies["cookiePage"].Value); if (cookies["cookiePageSize"] != null) inputModel.newPageSize = int.Parse(cookies["cookiePageSize"].Value); } } n.currentSortOrder = inputModel.newSortOrder; n.currentSortBy = inputModel.newSortBy; n.currentBeginWith = inputModel.newBeginWith; n.isSubmit = inputModel.isSubmit; n.currentPage = inputModel.newPage; n.currentPageSize = inputModel.newPageSize; if (productID != null) { using (ProductRepository rpt = new ProductRepository()) { int rowIndex = rpt.GetRowIndexByProductID(productID, n.currentSortBy, n.currentSortOrder); // determine the currentPage based on this rowIndex (150923) n.currentPage = (rowIndex / n.currentPageSize) + 1; } cookies["cookiePage"].Value = n.currentPage.ToString(); } else if (n.currentPage < 1) { n.currentPage = 1; } else if (inputModel.currentSortBy != inputModel.newSortBy || inputModel.currentSortOrder != inputModel.newSortOrder || inputModel.currentBeginWith != inputModel.newBeginWith || inputModel.currentPageSize != inputModel.newPageSize) { n.currentPage = 1; } int startRowIndex = (n.currentPage - 1) * n.currentPageSize; int maximumRows = n.currentPageSize; using (ProductRepository rpt = new ProductRepository()) { n.Products = rpt.GetProductListByName(n.currentBeginWith, n.currentSortBy, n.currentSortOrder, startRowIndex, maximumRows); n.numOfProducts = rpt.GetProductCountByName(n.currentBeginWith); } if (n.numOfProducts % n.currentPageSize == 0) n.numOfPages = (n.numOfProducts / n.currentPageSize); else n.numOfPages = ((n.numOfProducts / n.currentPageSize) + 1); return n; }