//GET: Store/Add to cart method// public ActionResult AddToCart(int AlbumId) { //to create unique cart id// String CurrentCartId; GetCartId(); CurrentCartId = Session["CartId"].ToString(); //create and save item into cart// //to check if item already exists in cart// Cart cart = db.Carts.SingleOrDefault(c => c.AlbumId == AlbumId && c.CartId == CurrentCartId); if(cart == null) { cart = new Cart { CartId = CurrentCartId, AlbumId = AlbumId, Count = 1, DateCreated = DateTime.Now }; db.Carts.Add(cart); } else { //increament count by 1// cart.Count++; } db.SaveChanges(); //redirect to cart page// return RedirectToAction("ShoppingCart"); }
public ActionResult Create([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album) { if (ModelState.IsValid) { //Save album cover if there is one if (Request.Files.Count > 0) { var file = Request.Files[0]; //If this is a legit file, save it if (file.FileName != null && file.ContentLength > 0 && file.ContentLength <= 2048000) { //This is the dynamic path that we want to save the image file string path = Server.MapPath("/Content/Images/") + file.FileName; //Save file into directory on server file.SaveAs(path); //Add new path to the database album.AlbumArtUrl = "/Content/Images/" + file.FileName; } } db.Albums.Add(album); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId); ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); return(View(album)); }
public ActionResult Create([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album) { if (ModelState.IsValid) { // upload cover image if any if (Request.Files.Count > 0) { var file = Request.Files[0]; if (file.FileName != null && file.ContentLength > 0) { if (file.ContentType.Equals("image/png") || file.ContentType.Equals("image/jpeg")) { string path = Server.MapPath("~/Content/Images/") + file.FileName; file.SaveAs(path); album.AlbumArtUrl = "/Content/Images/" + file.FileName; } else { return(View("Error")); } } } db.Albums.Add(album); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId); ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); return(View(album)); }
public ActionResult Create([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album) { if (ModelState.IsValid) { // check for a new cover image upload if (Request.Files.Count > 0) { var file = Request.Files[0]; if (file.FileName != null && file.ContentLength > 0) { string path = Server.MapPath("~/Content/Images/") + file.FileName; file.SaveAs(path); // add path to image name before saving album.AlbumArtUrl = "/Content/Images/" + file.FileName; } } db.Albums.Add(album); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId); ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); return(View(album)); }
public ActionResult Create([Bind(Include = "Id,GenreId,ArtistId,AlbumName,price,AlbumArtistUrl")] Album album) { if (ModelState.IsValid) { if (Request.Files.Count > 0) { var file = Request.Files[0]; if (file.FileName != null & file.ContentLength > 0) { string path = Server.MapPath("/Content/Images/") + file.FileName; file.SaveAs(path); album.AlbumArtistUrl = "/Content/Images/" + file.FileName; } } db.Albums.Add(album); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ArtistId = new SelectList(db.Artists, "Id", "Artist1", album.ArtistId); ViewBag.GenreId = new SelectList(db.Genres, "Id", "genre1", album.GenreId); return(View(album)); }
// GET: Store/AddToCart public ActionResult AddToCart(int AlbumId) { GetCartId(); string CurrentCartId = Session["CartId"].ToString(); //string CurrentCartId ="Monday-1-Cart-D1"; // check if the ablum is already in cart Cart cartItem = db.Carts.SingleOrDefault(c => c.CartId == CurrentCartId && c.AlbumId == AlbumId); if (cartItem == null) { cartItem = new Cart { AlbumId = AlbumId, Count = 1, DateCreated = DateTime.Now, CartId = CurrentCartId }; db.Carts.Add(cartItem); } else { cartItem.Count++; } //save //db.Carts.Add(cartItem); db.SaveChanges(); return(RedirectToAction("ShoppingCart")); }
//Get: Store/AddToCart/5 public ActionResult AddToCart(int AlbumId) { GetCartId(); string CurrentCartId = Session["CartId"].ToString(); Cart cartItem = db.Carts.SingleOrDefault(c => c.CartId == CurrentCartId && c.AlbumId == AlbumId); if (cartItem == null) { cartItem = new Cart { AlbumId = AlbumId, Count = 1, DateCreated = DateTime.Now, CartId = CurrentCartId }; db.Carts.Add(cartItem); } else { cartItem.Count++; } db.SaveChanges(); //show cart page return(RedirectToAction("ShoppingCart")); }
public ActionResult Create([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album) { if (ModelState.IsValid) { // set the placeholder in case there is no upload album.AlbumArtUrl = "/Content/Images/placeholder.gif"; // save new album cover if there is one if (Request.Files.Count > 0) { var file = Request.Files[0]; if (file.FileName != null && file.ContentLength > 0) { string path = Server.MapPath("/Content/Images/") + file.FileName; file.SaveAs(path); album.AlbumArtUrl = "/Content/Images/" + file.FileName; } } db.Albums.Add(album); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId); ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); return(View(album)); }
// GET: Store/AddToCart/5 public ActionResult AddToCart(int AlbumId) { // create new cart item GetCartId(); string CurrentCartId = Session["CartId"].ToString(); // check if album is already in the user's cart Cart cartItem = db.Carts.SingleOrDefault(c => c.CartId == CurrentCartId && c.AlbumId == AlbumId); if (cartItem == null) { cartItem = new Cart { AlbumId = AlbumId, Count = 1, DateCreated = DateTime.Now, CartId = CurrentCartId }; // save to db db.Carts.Add(cartItem); } else { // increment the cout cartItem.Count++; } db.SaveChanges(); // show cart page return(RedirectToAction("ShoppingCart")); }
//GET: STORE/AddRToCart public ActionResult AddToCart(int AlbumId) { //determine cardId to group this user's item together string CurrentCartId; GetCartId(); CurrentCartId = Session["CartId"].ToString(); //if item is already in the user's cart then icrement by 1 Cart cart = db.Carts.SingleOrDefault(c => c.AlbumId == AlbumId && c.CartId == CurrentCartId); if (cart == null) { //create and save the new cart item cart = new Cart { CartId = CurrentCartId, AlbumId = AlbumId, Count = 1, DateCreated = DateTime.Now }; db.Carts.Add(cart); } else { cart.Count++; } db.SaveChanges(); //load the shopping cart page return(RedirectToAction("ShoppingCart")); }
[ValidateAntiForgeryToken] //We validate this any time we post //POST: Checkout public ActionResult Checkout(FormCollection values) //FormCollection will include all values on the form { //create a new order and populate the fields from the form var order = new Order(); //This method will automatically map the values coming in to the model TryUpdateModel(order); //check the PromoCode for a value of "FREE" if (values["PromoCode"] != "FREE") { ViewBag.Message = "Use Promo Code FREE"; //Pass the view back, with the order (This will automatically repopulate the view) return(View(order)); } else { //Promo Code is good, so save the order //auto-fill the username, email, date, and total order.Username = User.Identity.Name; order.Email = User.Identity.Name; order.OrderDate = DateTime.Now; var cart = ShoppingCart.GetCart(this.HttpContext); order.Total = cart.GetTotal(); //Save Order to the database db.Orders.Add(order); db.SaveChanges(); //Save Order details from cart var cartItems = cart.GetCartItems(); foreach (Cart item in cartItems) { var orderDetail = new OrderDetail(); orderDetail.OrderId = order.OrderId; orderDetail.AlbumId = item.AlbumId; orderDetail.Quantity = item.Count; orderDetail.UnitPrice = item.Album.Price; db.OrderDetails.Add(orderDetail); } db.SaveChanges(); //Empty the users cart cart.EmptyCart(); } //Redirect to Order Details return(RedirectToAction("Details", "Orders", new { id = order.OrderId })); }
public ActionResult Create([Bind(Include = "GenreId,Name,Description")] Genre genre) { if (ModelState.IsValid) { db.Genres.Add(genre); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(genre)); }
public ActionResult Create([Bind(Include = "ArtistId,Name")] Artist artist) { if (ModelState.IsValid) { db.Artists.Add(artist); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(artist)); }
public ActionResult Post([FromBody] Album album) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.Albums.Add(album); db.SaveChanges(); return(CreatedAtAction("Post", album)); }
public ActionResult Create([Bind(Include = "OrderId,OrderDate,Username,FirstName,LastName,Address,City,State,PostalCode,Country,Phone,Email,Total")] Order order) { if (ModelState.IsValid) { db.Orders.Add(order); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(order)); }
public ActionResult Create([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album) { if (ModelState.IsValid) { db.Albums.Add(album); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId); ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); return(View(album)); }
// POST: AddressAndPayment public ActionResult AddressAndPayment(FormCollection values) { // instantiate a new Order var order = new Order(); // populate the Order properties with the matching form fields TryUpdateModel(order); // check the promotion code instead of payment if (string.Equals(values["PromoCode"], "FREE", StringComparison.OrdinalIgnoreCase) == false) { ViewBag.Message = "Invalid Promo Code. Please use the code 'FREE'."; return(View(order)); } // save the Order & get the new OrderId order.Username = User.Identity.Name; order.OrderDate = DateTime.Now; order.Email = User.Identity.Name; var cart = ShoppingCart.GetCart(this.HttpContext); order.Total = cart.GetTotal(); db.Orders.Add(order); db.SaveChanges(); // save the Order Details var cartItems = cart.GetItems(); foreach (Cart item in cartItems) { var orderDetail = new OrderDetail(); orderDetail.AlbumId = item.AlbumId; orderDetail.OrderId = order.OrderId; orderDetail.Quantity = item.Count; orderDetail.UnitPrice = item.Album.Price; // save the detail db.OrderDetails.Add(orderDetail); } // commit order detail save db.SaveChanges(); // empty the cart cart.EmptyCart(); // display Order Confirmation page return(RedirectToAction("OrderSummary", new { OrderId = order.OrderId })); }
// POST: Checkout -> when the user clicks on the Complete Order button public ActionResult Checkout(FormCollection values) { // create a new order and populate the fields from the form. var order = new Order(); TryUpdateModel(order); //It will try to match the values with the same ones on the Model // check the PromoCode for a value of "FREE" if (values["PromoCode"] != "FREE") { ViewBag.Message = "Use Promo Code FREE"; return(View(order)); } else { // Promo Code is good so save the order // auto-fill the username, email, date, and total order.Username = User.Identity.Name; order.Email = User.Identity.Name; order.OrderDate = DateTime.Now; var cart = ShoppingCart.GetCart(this.HttpContext); order.Total = cart.GetTotal(); // save db.Orders.Add(order); db.SaveChanges(); // save order details from cart var cartItems = cart.GetCartItems(); foreach (Cart item in cartItems) { var orderDetail = new OrderDetail(); orderDetail.OrderId = order.OrderId; orderDetail.AlbumId = item.AlbumId; orderDetail.Quantity = item.Count; orderDetail.UnitPrice = item.Album.Price; db.OrderDetails.Add(orderDetail); } db.SaveChanges(); //empty the user's cart cart.EmptyCart(); } // redirect to Order Details return(RedirectToAction("Details", "Orders", new { id = order.OrderId })); }
// GET: Store/AddToCart public ActionResult AddToCart(int AlbumId) { // identify the user's cart GetCartId(); //string CurrentCartId = Session["CartId"].ToString(); // save new item Cart CartItem = new Cart { CartId = Session["CartId"].ToString(), AlbumId = AlbumId, Count = 1, DateCreated = DateTime.Now }; db.Carts.Add(CartItem); db.SaveChanges(); // show the cart page return(RedirectToAction("ShoppingCart")); }
// GET: /addtocart/5 public ActionResult AddToCart(int AlbumId) { // check / generate CartId GetCartId(); string CurrentCartId = Session["CartId"].ToString(); // check if this album is already in this cart var cartItem = db.Carts.SingleOrDefault(c => c.AlbumId == AlbumId && c.CartId == CurrentCartId); if (cartItem == null) { // use the Cart model to add the album to the user's cart cartItem = new Cart { AlbumId = AlbumId, CartId = CurrentCartId, Count = 1, DateCreated = DateTime.Now }; db.Carts.Add(cartItem); } else { cartItem.Count++; } db.SaveChanges(); // commit insert or update // show the cart page return(RedirectToAction("Index")); }
public ActionResult AddressAndPayment(FormCollection values) { var order = new Order(); TryUpdateModel(order); try { if (string.Equals(values["PromoCode"], PromoCode, StringComparison.OrdinalIgnoreCase) == false) { return(View(order)); } else { order.Username = User.Identity.Name; order.Email = User.Identity.Name; //Process the order var cart = ShoppingCart.GetCart(this.HttpContext); order.Total = cart.GetTotal(); order.OrderDate = DateTime.Now; //Save Order db.Orders.Add(order); db.SaveChanges(); cart.CreateOrder(order); return(RedirectToAction("Complete", new { id = order.OrderId })); } } catch { //Invalid - redisplay with errors return(View(order)); } }
// GET: /addtocart/5 public ActionResult AddToCart(int AlbumId) { // generate CartId if empty GetCartId(); string currentCartId = Session["CartId"].ToString(); // is album already in cart? var cartItem = db.Carts.SingleOrDefault( c => c.CartId == currentCartId && c.AlbumId == AlbumId); if (cartItem == null) { // add the item to the current cart cartItem = new Cart { AlbumId = AlbumId, Count = 1, CartId = Session["CartId"].ToString(), DateCreated = DateTime.Now }; db.Carts.Add(cartItem); } else { // album already in this user's cart, so add 1 to the current count cartItem.Count++; } // save the insert or update db.SaveChanges(); // show the cart view return(RedirectToAction("Index")); }