public ActionResult Approve(int productID, int vendorID) { using (var db = new WebDauGiaEntities()) { var pro = db.Products.Find(productID); pro.Status = 1; pro.DateEnd = DateTime.Now.AddDays(7); db.SaveChanges(); //khởi tạo nội dung mail var user = db.Users.Find(vendorID); var image = db.Photos.Where(p => p.ProductID == productID).FirstOrDefault().Name; image = image.Substring(2).Trim(); var body = new StringBuilder(); body.Append($"<h3>Chào {user.Name} !</h2>"); body.Append($"<h1 style='color:blue'>Chúc mừng bạn của bạn đăng đã được duyệt.</h1>"); body.Append($"<h1 style='color:blue'>Sản phẩm {pro.Name}</h1>"); body.Append($"<img style='width:300px;height:400px;' src=cid:MyPic>"); body.Append($"<h3 style='color:green;'>Ngày đăng của sản phẩm: {pro.DateStart}$</h3>"); body.Append($"<h3>Sản phẩm sẽ được đăng trong vòng 7 ngày kể từ ngày được duyệt.</h3>"); body.Append($"<h3>Thời gian kết thúc đấu giá của sản phẩm là: <span style='color:red;'>{pro.DateEnd}</span></h3>"); body.Append($"<h3>Bạn có thể xem chi tiết sản phẩm <a href='http://localhost:62902/Product/Detail?productID={pro.ID}'>tại đây.</a></h3>"); LinkedResource yourPictureRes = new LinkedResource(Path.Combine(HttpRuntime.AppDomainAppPath, image)); yourPictureRes.ContentId = "MyPic"; Email.Send(user.Email, "[Thế Giới Ngầm] Chúc mừng bạn sản phẩm đã được duyệt.", body, yourPictureRes); string notify = "Duyệt sản phẩm thành công !"; return(RedirectToAction("Approve", "Admin", new { message = notify })); } }
public static bool IsLogged() { var flag = HttpContext.Current.Session["isLogin"]; if (flag == null || (int)flag == 0) { // Kiểm tra trong cookie // nếu có cookie , dùng thông tin trong cookie // để tái tạo lại session if (HttpContext.Current.Request.Cookies["userID"] != null) { int userIdCookie = Convert.ToInt32(HttpContext.Current.Request.Cookies["userID"].Value); using (var db = new WebDauGiaEntities()) { var user = db.Users.Where(u => u.ID == userIdCookie).FirstOrDefault(); HttpContext.Current.Session["isLogin"] = 1; HttpContext.Current.Session["user"] = user; return(true); } } else { return(false); } } else { return(true); } }
public ActionResult ApproveSale(int?index, string message) { ViewBag.Message = message; if (!index.HasValue) { index = 1; } using (var db = new WebDauGiaEntities()) { var list = (from u in db.Users where u.SaleStatus == 1 select new ManageUserViewModel { ID = u.ID, Name = u.Name, UserName = u.UserName, Address = u.Address, Credits = u.Credits, Email = u.Email, Negative = u.Negative, Positive = u.Positive, Image = u.Photo }).ToList(); int n = list.Count(); int take = 6; ViewBag.Pages = Math.Ceiling((double)n / take); ViewBag.Previous = index - 1 < 1 ? 1 : index - 1; ViewBag.Next = index + 1 > ViewBag.Pages ? ViewBag.Pages : index + 1; list = list.OrderBy(p => p.ID).Skip((int)(index - 1) * take).Take(take).ToList(); return(View(list)); } }
public ActionResult DangKy(ThanhVien model) { if (!ModelState.IsValid) { // TODO: Captcha validation failed, show error message ViewBag.ErrorMsg = "Bạn nhập sai Captcha!"; return(View()); } else { // TODO: Captcha validation passed, proceed with protected action ThanhVien m = new ThanhVien { TenDangNhap = model.TenDangNhap, MatKhau = StringUtils.Md5(model.MatKhau), Email = model.Email, HoTen = model.HoTen, DiaChi = model.DiaChi }; using (WebDauGiaEntities ctx = new WebDauGiaEntities()) { ctx.ThanhViens.Add(m); ctx.SaveChanges(); } return(RedirectToAction("DangNhap", "User")); } }
public ActionResult ResetPasswordUser(int?userID) { if (!userID.HasValue) { return(View("Error")); } else { using (var db = new WebDauGiaEntities()) { var user = db.Users.Find(userID); // gửi pasword mới cho user var newPWD = StringUtils.RandomString(6); var body = new StringBuilder(); body.Append($"<h3>Chào {user.Name} !</h2>"); body.Append($"<h1 style='color:blue;'>Password của bạn đã được reset.</h1>"); body.Append($"<h3 >Password mới của bạn là: <span style='color:red;'>{newPWD}<span></h3>"); Email.Send(user.Email, "[Thế Giới Ngầm] Reset password.", body); user.Password = StringUtils.Md5(newPWD); db.SaveChanges(); string notify = "Reset password thành công . Password mới đã được gửi vào email của User!"; return(RedirectToAction("ManageUser", new { message = notify })); } } }
/// <summary> /// Lấy danh sách hình ảnh của sản phẩm /// </summary> /// <param name="productID">ID sản phẩm</param> /// <returns>Trả về partial view PostImagePartialView</returns> public ActionResult PostImage(int?productID) { using (var db = new WebDauGiaEntities()) { var list = db.Photos.Where(p => p.ProductID == productID).ToList(); return(PartialView("_PostImagePartialView", list)); } }
/// <summary> /// Hiển thị danh sách category trên navbar /// </summary> /// <returns></returns> public ActionResult Index() { using (var db = new WebDauGiaEntities()) { var list = db.Categories.Where(c => c.Status == true).ToList(); return(PartialView("_NavbarPartial", list)); } }
/// <summary> /// Lấy danh sách tên tất cả các sản phẩm để autocomplete /// </summary> /// <param name="name">từ khoá tìm kiếm</param> /// <returns></returns> public JsonResult Autocomplete(string name) { WebDauGiaEntities ctx = new WebDauGiaEntities(); List <string> pro; pro = ctx.Products.Where(x => x.Name.StartsWith(name)) .Select(e => e.Name.Trim()).Distinct().ToList(); return(Json(pro, JsonRequestBehavior.AllowGet)); }
public ActionResult RemoveCategory(int id) { using (var db = new WebDauGiaEntities()) { var cate = db.Categories.Find(id); cate.Status = false; db.SaveChanges(); string notify = "Đã xóa thể loại thành công !"; return(RedirectToAction("ManageCategory", new { message = notify })); } }
public ActionResult EditCategory(Category item) { using (var db = new WebDauGiaEntities()) { var cate = db.Categories.Find(item.ID); cate.Name = item.Name; db.SaveChanges(); ViewBag.Success = "Chỉnh sửa tên thể loại thành công !"; return(View(cate)); } }
public ActionResult AddCategory(Category item) { using (var db = new WebDauGiaEntities()) { item.Status = true; db.Categories.Add(item); db.SaveChanges(); string spDirPath = Server.MapPath("~/images/products"); string targetDirpath = Path.Combine(spDirPath, item.ID.ToString()); Directory.CreateDirectory(targetDirpath); string notify = "Đã thêm thể loại thành công !"; return(RedirectToAction("ManageCategory", new { message = notify })); } }
public ActionResult CheckFavourites(int productID) { var userID = CurrentContext.GetUser().ID; using (var db = new WebDauGiaEntities()) { var query = db.WatchLists.Where(w => w.UserID == userID && w.ProductID == productID).FirstOrDefault(); bool liked = false; if (query != null) { liked = true; } return(PartialView("_ButtonFavouritesPartialView", liked)); } }
public ActionResult EditCategory(int?id) { if (!id.HasValue) { return(View("Error")); } else { using (var db = new WebDauGiaEntities()) { var cate = db.Categories.Find(id); return(View(cate)); } } }
public static CommentVendor IsCommentVendor(int vendorID, int productID) { int userID = GetUser().ID; using (var db = new WebDauGiaEntities()) { var cm = db.CommentVendors.Where(c => c.ProductID == productID && c.VendorID == vendorID && c.WinnerID == userID).FirstOrDefault(); if (cm != null) { return(cm); } else { return(null); } } }
/// <summary> /// GET: Home/Index /// </summary> /// <returns>Truyền ra view Top 5 sản phẩm giá cao nhất, nhiều lượt ra giá nhất, gần kết thúc </returns> public ActionResult Index() { using (var db = new WebDauGiaEntities()) { // Kiểm tra sản phẩm hết hạn AccountController.CheckTimeOut(db); // Truy vấn lấy thông tin tất cả sản phẩm var list = (from p in db.Products join photo in db.Photos on p.ID equals photo.ProductID join c in db.Categories on p.CategoryID equals c.ID join u in db.Users on p.Winner equals u.ID where p.Status == 1 select new ProductHomeViewModel { ID = p.ID, Name = p.Name.Trim(), Winner = u.Name.Trim(), Price = p.PriceAuction, DateEnd = p.DateEnd, DateStart = p.DateStart, LinkImage = photo.Name, ImageUser = u.Photo, BuyItNow = p.BuyNowPice, CateID = c.ID, CateName = c.Name, WinnerID = u.ID, Status = p.Status, VendorID = p.VendorID }).ToList(); // vì khi join với bảng photo thì 1 sản phẩm có thể có nhiều hình // mình chỉ cần lấy 1 hình đầu tiên làm hình đại diện // nên cần distinct theo ID sản phẩm list = list.DistinctBy(p => p.ID).ToList(); // Sắp xếp giảm dần theo giá và lấy 8 dòng đầu tiên ViewBag.TopPrice = list.OrderByDescending(p => p.Price).Take(8).ToList(); // Sắp xếp giảm theo thời gian theo giá và lấy 8 dòng đầu tiên ViewBag.TopDateEnd = list.OrderBy(p => p.DateEnd).Take(8).ToList(); // Lấy 5 sản phẩm có số lượt ra giá nhiều nhất ViewBag.TopAuction = GetTopAuction(); return(View()); } }
public ActionResult Edit(EditProductViewModel model) { using (var db = new WebDauGiaEntities()) { var pro = db.Products.Find(model.ProductID); pro.Descrition = pro.Descrition + "<br>" + model.Des; db.SaveChanges(); ViewBag.MsgSuccess = "Chỉnh sửa thông tin sản phẩm thành công !"; var list = (from p in db.Products join v in db.Users on p.VendorID equals v.ID join w in db.Users on p.Winner equals w.ID where p.ID == model.ProductID select new ProductDetailViewModel { ID = p.ID, Name = p.Name, VendorID = v.ID, VendorName = v.Name.Trim(), VendorImage = v.Photo, Price = p.Price, Description = p.Descrition, DateStart = p.DateStart, DateEnd = p.DateEnd, PriceAuction = p.PriceAuction, WinnerID = w.ID, WinnerName = w.Name.Trim(), StepPrice = p.StepPrice, BuyNowPice = p.BuyNowPice, Status = p.Status, FeedbackVendor = (int)(((double)(v.Positive + v.Negative + 1) / (v.Positive + 1)) * 100), FeedbackWinner = (int)(((double)(w.Positive + w.Negative + 1) / (w.Positive + 1)) * 100), }).FirstOrDefault(); var listUser = (from h in db.HistoryAuctions join u in db.Users on h.UserID equals u.ID where h.ProductID == model.ProductID && h.Denied == 0 select new HistoryAuctionProductVM { UserID = h.UserID, UserName = u.Name, DateAction = h.DateAction }).ToList(); ViewBag.History = listUser.ToList(); return(View(list)); } }
/// <summary> /// Lấy Top 5 sản phẩm có nhiều lượt ra giá nhất /// </summary> /// <returns> List<ProductHomeViewModel> </returns> public List <ProductHomeViewModel> GetTopAuction() { using (var db = new WebDauGiaEntities()) { // truy vấn lấy ra ID sản phẩm và số lượt đấu giá của sản phẩm đó từ bảng HistoryAuctions // Kết quả sẽ có được 1 bảng gồm 2 cột ID sản phẩm và số lượt đấu giá của sản phẩm var history = (from h in db.HistoryAuctions join p in db.Products on h.ProductID equals p.ID where p.Status == 1 group h by h.ProductID into his select new { ProductId = his.Key, Num = his.Count() }).ToList(); // Sắp xếp bảng vừa lấy được giảm dần và lấy 5 dòng đầu tiên history = history.OrderByDescending(h => h.Num).Take(8).ToList(); // Truy vấn để lấy thông tin của của sản phẩm để truyền ra view var topAuction = (from h in history join p in db.Products on h.ProductId equals p.ID join c in db.Categories on p.CategoryID equals c.ID join u in db.Users on p.Winner equals u.ID join photo in db.Photos on p.ID equals photo.ProductID where p.Status == 1 select new ProductHomeViewModel { ID = p.ID, Name = p.Name.Trim(), Winner = u.Name.Trim(), Price = p.PriceAuction, DateEnd = p.DateEnd, DateStart = p.DateStart, LinkImage = photo.Name, ImageUser = u.Photo, BuyItNow = p.BuyNowPice, CateID = c.ID, CateName = c.Name, WinnerID = u.ID, VendorID = p.VendorID }).ToList(); // vì khi join với bảng photo thì 1 sản phẩm có thể có nhiều hình // mình chỉ cần lấy 1 hình đầu tiên làm hình đại diện // nên cần distinct theo ID sản phẩm topAuction = topAuction.DistinctBy(p => p.ID).ToList(); return(topAuction); } }
public ActionResult Approve(int?index, string message) { ViewBag.Message = message; if (!index.HasValue) { index = 1; } else { } using (var db = new WebDauGiaEntities()) { var list = (from p in db.Products join v in db.Users on p.VendorID equals v.ID join photo in db.Photos on p.ID equals photo.ProductID where p.Status == 0 select new ProductApproveViewModel { ID = p.ID, Name = p.Name, VendorID = p.VendorID, VendorName = v.Name, Image = photo.Name, Des = p.Descrition, Price = p.Price, StepPrice = p.StepPrice, DateStart = p.DateStart, BuyNowPice = p.BuyNowPice, AutoExtend = p.AutoExtend }).ToList(); list = list.DistinctBy(p => p.ID).ToList(); int n = list.Count(); int take = 6; ViewBag.Pages = Math.Ceiling((double)n / take); ViewBag.Previous = index - 1 < 1 ? 1 : index - 1; ViewBag.Next = index + 1 > ViewBag.Pages ? ViewBag.Pages : index + 1; list = list.OrderBy(p => p.DateStart).Skip((int)(index - 1) * take).Take(take).ToList(); return(View(list)); } }
public ActionResult DangNhap(ThanhVien model) { string pass = StringUtils.Md5(model.MatKhau); using (var ctx = new WebDauGiaEntities()) { var member = ctx.ThanhViens .Where(u => u.TenDangNhap == model.TenDangNhap && u.MatKhau == pass) .FirstOrDefault(); if (member != null) { Session["isLogin"] = 1; return(RedirectToAction("Index", "Home")); } else { ViewBag.ErrorMsg = "Đăng nhập thất bạn"; return(View()); } } }
public ActionResult RemoveUser(int?userID) { if (!userID.HasValue) { return(View("Error")); } using (var db = new WebDauGiaEntities()) { var user = db.Users.Find(userID); user.Status = 0; db.SaveChanges(); var body = new StringBuilder(); body.Append($"<h3>Chào {user.Name} !</h2>"); body.Append($"<h1 style='color:red;'>Cảnh báo tài khoản của bạn đã bị xóa.</h1>"); body.Append($"<h3 style='color:red;'>Vì một số lý do tài khoản của bạn đã bị xóa.</h3>"); Email.Send(user.Email, "[Thế Giới Ngầm] Cảnh báo tài khoản của bạn đã bị xóa.", body); string notify = "Đã xóa user thành công !"; return(RedirectToAction("ManageUser", new { message = notify })); } }
public ActionResult ApproveSale(int userID) { using (var db = new WebDauGiaEntities()) { var user = db.Users.Find(userID); user.SaleStatus = 0; user.DateStartSale = DateTime.Now; user.DateEndSale = DateTime.Now.AddDays(7); db.SaveChanges(); string notify = "Đã duyệt thành công !"; // khởi tạo nội dung mail var body = new StringBuilder(); body.Append($"<h3>Chào {user.Name} !</h2>"); body.Append($"<h1 style='color:blue'>Chúc mừng bạn đã trở thành người bán hàng.</h1>"); body.Append($"<h3>Bạn sẽ được đăng sản phẩm trong vòng 7 ngày kể từ ngày được duyệt.</h3>"); body.Append($"<h3>Thời gian bắt đầu: <span style='color:red;'>{user.DateEndSale}</span></h3>"); body.Append($"<h3>Thời gian kết thúc: <span style='color:red;'>{user.DateEndSale}</span></h3>"); Email.Send(user.Email, "[Thế Giới Ngầm] Chúc mừng bạn đã trở thành người bán hàng.", body); return(RedirectToAction("ApproveSale", "Admin", new { message = notify })); } }
public ActionResult ManageCategory(string message, int?index) { if (!index.HasValue) { index = 1; } else { } ViewBag.Message = message; using (var db = new WebDauGiaEntities()) { var list = db.Categories.Where(u => u.Status == true).ToList(); int n = list.Count(); int take = 6; ViewBag.Pages = Math.Ceiling((double)n / take); ViewBag.Previous = index - 1 < 1 ? 1 : index - 1; ViewBag.Next = index + 1 > ViewBag.Pages ? ViewBag.Pages : index + 1; list = list.OrderBy(c => c.ID).Skip((int)(index - 1) * take).Take(take).ToList(); return(View(list)); } }
/// <summary> /// GET: Product/Detail /// Trả về thông tin chi tiết sản phẩm /// </summary> /// <param name="productID">id sản phẩm</param> /// <param name="message">dùng để nhận thông báo trả về view detail từ 1 hàm khác</param> /// <returns>trả về view Detail hoặc view edit nếu người dùng hiện tại là người đăng sản phẩm</returns> public ActionResult Detail(int?productID, string msgerror, string msgsuccess) { // khi productID == null thì chuyển về trang chủ if (productID == null) { return(RedirectToAction("Index", "Home")); } //Dùng để nhận thông báo từ 1 hàm khác và xuất thông báo ra view ViewBag.MsgError = msgerror; ViewBag.MsgSuccess = msgsuccess; using (var db = new WebDauGiaEntities()) { // Lấy id của người dùng hiện tại nếu người dùng đã đăng nhập // nếu người dùng chưa đăng nhập thì id = 0 var uid = 0; if (CurrentContext.IsLogged()) { uid = CurrentContext.GetUser().ID; } // Kiểm tra sản phẩm hiện tại có phải là sản phẩm yêu thích của người dùng hay không // Nếu là có thì biến watching sẽ là true ngược lại là false // Nếu người dùng chưa đăng nhập // hoặc sản phẩm hiện tại không phải là sản phẩm yêu thích của người dùng // thì danh sách trả về sẽ là danh sách rỗng var watch = db.WatchLists.Where(w => w.UserID == uid && w.ProductID == productID).ToList(); bool watching = false; if (watch.Count != 0) { watching = true; } // truy vấn lấy thông tin chi tiết của sản phẩm var list = (from p in db.Products join v in db.Users on p.VendorID equals v.ID join w in db.Users on p.Winner equals w.ID where p.ID == productID select new ProductDetailViewModel { ID = p.ID, Name = p.Name.Trim(), VendorID = v.ID, VendorName = v.Name.Trim(), VendorImage = v.Photo, Price = p.Price, Description = p.Descrition, DateStart = p.DateStart, DateEnd = p.DateEnd, PriceAuction = p.PriceAuction, WinnerID = w.ID, WinnerName = w.Name.Trim(), StepPrice = p.StepPrice, BuyNowPice = p.BuyNowPice, Watching = watching, Status = p.Status, FeedbackVendor = (int)(((double)(v.Positive + v.Negative + 1) / (v.Positive + 1)) * 100), FeedbackWinner = (int)(((double)(w.Positive + w.Negative + 1) / (w.Positive + 1)) * 100), }).FirstOrDefault(); // Kiểm tra nếu người dùng đã đăng nhập if (uid != 0) { // Kiểm tra xem nếu người dùng đã đăng nhập // thì người dùng có đang bị cấm đấu giá trên sản phẩm hiện tại hay không ViewBag.DeniedProduct = db.DeniedProducts.Where(d => d.UserID == uid && d.ProductID == productID).FirstOrDefault(); // Kiểm tra xem nếu người dùng đã đăng nhập // thì người dùng có đang bị cấm đấu giá trên các sản phẩm của người đăng hay không ViewBag.DeniedVendor = db.DeniedVendors.Where(d => d.UserID == uid && d.VendorID == list.VendorID).FirstOrDefault(); // Kiểm tra nếu sản phẩm hiện tại là sản phẩm do người dùng hiện tại đăng // thì sẽ trả về trang chỉnh sửa thông tin sản phẩm if (uid == list.VendorID && list.Status == 1) { return(RedirectToAction("Edit", "Product", new { productID = productID, message = msgsuccess })); } } return(View(list)); } }
public ActionResult Edit(int?productID, int?index, string message) { ViewBag.MsgSuccess = message; if (!productID.HasValue) { return(RedirectToAction("Index", "Home")); } if (!index.HasValue) { index = 1; } //lấy id user đang đăng nhập var userID = CurrentContext.GetUser().ID; using (var db = new WebDauGiaEntities()) { // lấy thông tin sản phẩm có id = productID và có VendorID = userID var pro = (from p in db.Products join v in db.Users on p.VendorID equals v.ID join w in db.Users on p.Winner equals w.ID where p.ID == productID && p.VendorID == userID select new ProductDetailViewModel { ID = p.ID, Name = p.Name, VendorID = v.ID, VendorName = v.Name.Trim(), VendorImage = v.Photo, Price = p.Price, Description = p.Descrition, DateStart = p.DateStart, DateEnd = p.DateEnd, PriceAuction = p.PriceAuction, WinnerID = w.ID, WinnerName = w.Name.Trim(), StepPrice = p.StepPrice, BuyNowPice = p.BuyNowPice, Status = p.Status, FeedbackVendor = (int)(((double)(v.Positive + v.Negative + 1) / (v.Positive + 1)) * 100), FeedbackWinner = (int)(((double)(w.Positive + w.Negative + 1) / (w.Positive + 1)) * 100), }).FirstOrDefault(); // Lấy danh sách user tham gia đấu giá sản phẩm var listUser = (from h in db.HistoryAuctions join u in db.Users on h.UserID equals u.ID where h.ProductID == productID && h.Denied == 0 select new HistoryAuctionProductVM { UserID = h.UserID, UserName = u.Name, DateAction = h.DateAction }).ToList(); // phân trang danh sách user vừa tìm được int n = listUser.Count(); int take = 6; ViewBag.Pages = Math.Ceiling((double)n / take); ViewBag.Previous = index - 1 < 1 ? 1 : index - 1; ViewBag.Next = index + 1 > ViewBag.Pages ? ViewBag.Pages : index + 1; ViewBag.History = listUser.OrderBy(h => h.DateAction).Skip((int)(index - 1) * take).Take(take).ToList(); return(View(pro)); } }
/// <summary> /// GET: Product/ByCategory /// </summary> /// <param name="id">ID của category</param> /// <param name="memid">memid dùng cho sắp xếp mặc định thì sắp xếp theo ID ; 1 thì sắp xếp giá tăng dần ; 2 thì sắp xếp theo thời gian kết thúc giảm dần </param> /// <param name="index">số trang hiện tại</param> /// <returns>Danh sách sản phẩm theo thể loại ra View</returns> /// public ActionResult ByCategory(int?catid, int?memid, int?index) { if (catid.HasValue) { // Nếu trang hiện tại là null thì gán là 1 if (!index.HasValue) { index = 1; } else { } using (var db = new WebDauGiaEntities()) { AccountController.CheckTimeOut(db); // Lấy danh sách các sản phẩm có categoryID = id // vì khi join với bảng photo thì 1 sản phẩm có thể có nhiều hình // mình chỉ cần lấy 1 hình đầu tiên làm hình đại diện // nên cần distinct theo ID sản phẩm var list = (from p in db.Products join photo in db.Photos on p.ID equals photo.ProductID join u in db.Users on p.Winner equals u.ID join c in db.Categories on p.CategoryID equals c.ID where p.CategoryID == catid && p.Status == 1 select new ProductHomeViewModel { ID = p.ID, Name = p.Name, Winner = u.Name.Trim(), Price = p.PriceAuction, DateEnd = p.DateEnd, DateStart = p.DateStart, LinkImage = photo.Name, ImageUser = u.Photo, BuyItNow = p.BuyNowPice, CateID = c.ID, CateName = c.Name, WinnerID = u.ID, VendorID = p.VendorID }).DistinctBy(p => p.ID).ToList(); // lấy số lượng sản phẩm thuộc thể loai có ID = id int n = ViewBag.Count = list.Count; // Số lượng sản phẩm xuất hiện trong 1 trang int take = 6; // Tính số lượng trang = số lượng sản phẩm / số sản phẩm trong 1 trang // Làm tròn lên ví dụ 5.3 thành 6 và gán cho ViewBag để truyền ra view ViewBag.Pages = Math.Ceiling((double)n / take); // Lấy giá trị trang trước nếu nhỏ hơn 1 thì gán là 1 // ngược lại thì lấy index - 1 ViewBag.Previous = index - 1 < 1 ? 1 : index - 1; // Lấy giá trị trang sau nếu lớn hơn tổng số trang thì gán bằng tổng số trang // ngược lại thì lấy index + 1 ViewBag.Next = index + 1 > ViewBag.Pages ? ViewBag.Pages : index + 1; // nếu menid có giá trị thì sắp xếp theo giá hoặc theo thời gian // ngược lại sắp xếp theo id if (memid.HasValue) { // Dùng cho sắp xếp nếu memid = 1 thì sắp theo giá tăng dần // memid = 2 thì sắp theo giá tăng dần // memid = 3 thì sắp theo thời gian kết thúc giảm dần // memid = 4 thì sắp theo thời gian kết thúc tăng dần if (memid == 1) { list = list.OrderBy(p => p.Price).Skip((int)(index - 1) * take).Take(take).ToList(); } else if (memid == 2) { list = list.OrderByDescending(p => p.Price).Skip((int)(index - 1) * take).Take(take).ToList(); } else if (memid == 3) { list = list.OrderBy(p => p.DateEnd).Skip((int)(index - 1) * take).Take(take).ToList(); } else { list = list.OrderByDescending(p => p.DateEnd).Skip((int)(index - 1) * take).Take(take).ToList(); } } else { // Có phân trang ví dụ trang hiện tại là 1 thì chỉ lấy từ sản phẩm thứ 1->5 // nếu trang hiện tại là 2 thì chỉ lấy từ sản phẩm thứ 6->10 list = list.OrderBy(p => p.ID).Skip((int)(index - 1) * take).Take(take).ToList(); } //Truyền categoryID và MemID ra View để dùng phân trang sắp xếp ViewBag.CateID = catid; ViewBag.MemID = memid; return(View(list)); } } else { return(RedirectToAction("Index", "Home")); } }