public ActionResult AddToCart(int ProductId, int Quantity) { using (var db = new OnlineShopDb()) { // TÌM PRODUCT THEO ProductId var product = db.Products.Find(ProductId); if (product != null) { // XE HÀNG var shoppingCart = new ShoppingCart(); if (Session["ShoppingCart"] != null) { shoppingCart = (ShoppingCart)Session["ShoppingCart"]; } shoppingCart.AddToCart(product, Quantity); Session["ShoppingCart"] = shoppingCart; return(RedirectToAction("CheckOut", "ShoppingCart")); } return(HttpNotFound()); } }
public ActionResult CheckOut([Bind(Include = "ContactName,ContactAddress,ContactPhone,ContactEmail")] Order order) { using (var db = new OnlineShopDb()) { order.CreatedDate = DateTime.Now; order.Status = "PENDING"; order.CustomerId = null; // var userId = User.Identity.GetUserId(); order.Code = Guid.NewGuid().ToString(); var shoppingCart = (ShoppingCart)Session["ShoppingCart"]; foreach (var item in shoppingCart.ShoppingCartItems) { var orderDetail = new OrderDetail(); orderDetail.ProductId = item.Item.Id; orderDetail.Quantity = item.Quantity; orderDetail.Discount = item.Item.Discount; orderDetail.Price = item.Item.Price; order.OrderDetails.Add(orderDetail); } db.Orders.Add(order); db.SaveChanges(); Session["ShoppingCart"] = null; return(RedirectToAction("Index", "Home")); } }
public ActionResult DeleteProductImage(int deleteId) { using (var db = new OnlineShopDb()) { var item = db.ProductImages.Find(deleteId); if (item != null) { // XÓA HÌNH var pathToDetele = Server.MapPath(item.ImageUrl); if (System.IO.File.Exists(pathToDetele)) { System.IO.File.Delete(pathToDetele); } // XÓA TRONG DATABASE var productId = item.ProductId; db.ProductImages.Remove(item); db.SaveChanges(); return(RedirectToAction("UploadDetailsImages", "ManageProducts", new { id = productId })); } return(HttpNotFound()); } }
public ActionResult DisplayByCategory(int categoryId) { using (var db = new OnlineShopDb()) { var products = db.Products.Where(x => x.CategoryId == categoryId).ToList(); return(View("Index", products)); } }
// TUNG.NGO public ActionResult UploadDetailsImages(int id) { using (var db = new OnlineShopDb()) { var productImages = db.ProductImages.Where(x => x.ProductId == id).ToList(); return(View(productImages)); } }
public ActionResult Index(string sort) { using (var db = new OnlineShopDb()) { var products = db.Products.OrderBy(x => x.Price);; if (sort == "desc") { products = products.OrderByDescending(x => x.Price); } return(View(products.ToList())); } }
public ActionResult Details(int?id) { using (var db = new OnlineShopDb()) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Product product = db.Products.Include(x => x.ProductImages).FirstOrDefault(x => x.Id == id); if (product == null) { return(HttpNotFound()); } return(View(product)); } }
public void updateCart(int ProductId, int Quantity) { using (var db = new OnlineShopDb()) { // TÌM PRODUCT THEO ProductId var product = db.Products.Find(ProductId); if (product != null) { // XE HÀNG var shoppingCart = new ShoppingCart(); if (Session["ShoppingCart"] != null) { shoppingCart = (ShoppingCart)Session["ShoppingCart"]; } shoppingCart.UpdateCart(product, Quantity); Session["ShoppingCart"] = shoppingCart; } } }
public ActionResult Search(string SearchText, string PriceMin, string PriceMax) { using (var db = new OnlineShopDb()) { var min = 0; var max = Int32.MaxValue; if (string.IsNullOrEmpty(PriceMin) == false) { min = Convert.ToInt32(PriceMin); } if (string.IsNullOrEmpty(PriceMax) == false) { max = Convert.ToInt32(PriceMax); } var products = db.Products.Where(x => x.Name.Contains(SearchText) && (x.Price >= min && x.Price <= max)).ToList(); return(View("Index", products)); } }
public ActionResult RemoveCart(int ProductId) { using (var db = new OnlineShopDb()) { // TÌM PRODUCT THEO ProductId var product = db.Products.Find(ProductId); if (product != null) { // XE HÀNG var shoppingCart = new ShoppingCart(); if (Session["ShoppingCart"] != null) { shoppingCart = (ShoppingCart)Session["ShoppingCart"]; } shoppingCart.RemoveCart(product.Id); Session["ShoppingCart"] = shoppingCart; } } return(View("CheckOut")); }
public GenericRepository() { this.context = new OnlineShopDb(); this.dbSet = context.Set <TEntity>(); }