public IHttpActionResult PutArtist(int id, Artist artist) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != artist.ArtistId) { return(BadRequest()); } _db.Entry(artist).State = EntityState.Modified; try { _db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ArtistExists(id)) { return(NotFound()); } throw; } return(StatusCode(HttpStatusCode.NoContent)); }
public IHttpActionResult PutGenre(int id, Genre genre) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != genre.GenreId) { return(BadRequest()); } _db.Entry(genre).State = EntityState.Modified; try { _db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!GenreExists(id)) { return(NotFound()); } throw; } return(StatusCode(HttpStatusCode.NoContent)); }
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.OrderDate = DateTime.Now; db.Orders.Add(order); db.SaveChanges(); } } catch (Exception e) { return(View(order)); } //return View(); return(RedirectToAction("Complete", new { id = order.OrderId })); }
public ActionResult AddToCart(int id) { var addAlbum = db.Albums.Single(x => x.AlbumId == id); //确定需要添加至购物车的Album数据 var cart = ShoppingCart.GetCart(db, this.HttpContext); //获取购物车列表 cart.AddToCart(addAlbum); //将Album数据添加至购物车 db.SaveChanges(); //实例化数据 return(RedirectToAction("Index")); }
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 = "ArtistID,Name")] Artist artist) { if (ModelState.IsValid) { db.Artists.Add(artist); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(artist)); }
public ActionResult Create([Bind(Include = "StudentId,StuNo,Name,Gender,height,weight")] Student student) { if (ModelState.IsValid) { db.Students.Add(student); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(student)); }
public ActionResult Create(Album album) { if (ModelState.IsValid) { db.Albums.Add(album); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(album)); }
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 = "UserId,UserName,Password")] User user) { if (ModelState.IsValid) { db.Users.Add(user); db.SaveChanges(); //return RedirectToAction("Index"); } return(View()); //return View(user); }
public ActionResult Create([Bind(Include = "AlbumId,GenreId,AritstId,Title,Price,AlbumArtUrl")] Album album) { if (ModelState.IsValid) { db.Albums.Add(album); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); return(View(album)); }
public ActionResult Create(Album album) { if (ModelState.IsValid) { db.Albums.Add(album); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId); return(View(album)); }
public ActionResult Create([Bind(Include = "MusicId,Name,Description,Price,ArtistId,CategoryId")] Music music) { if (ModelState.IsValid) { db.Musics.Add(music); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", music.ArtistId); ViewBag.CategoryId = new SelectList(db.Categorys, "CategoryId", "Name", music.CategoryId); return(View(music)); }
public ActionResult Buy(int id) { var userName = User.Identity.Name; if (userName == null || userName == "") { userName = "******"; } var album = db.Albums.Find(id); var cart = db.Carts.SingleOrDefault(c => c.UserName == userName); if (cart == null) { cart = new Cart { UserName = userName, CartItems = new List <OrderDetail>() }; db.Carts.Add(cart); } else { cart.CartItems = db.OrderDetails.Include(od => od.Album).Where(od => od.CartId == cart.CartId).ToList(); } if (album != null) { var cartItem = cart.CartItems.Find(item => (item.AlbumId == album.AlbumId)); if (cartItem == null) { cartItem = new OrderDetail { AlbumId = album.AlbumId, Album = album, Quantity = 1 }; cart.CartItems.Add(cartItem); db.OrderDetails.Add(cartItem); } else { cartItem.Quantity++; db.Entry(cartItem).State = EntityState.Modified; } } db.SaveChanges(); return(PartialView("Cart", cart.CartItems)); }
public ActionResult Create([Bind(Include = "GenreId,Name,Description")] Genre genre) { try { if (ModelState.IsValid) { db.Genres.Add(genre); db.SaveChanges(); return(RedirectToAction("Index")); } } catch { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator."); } return(View(genre)); }
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.OrderDate = DateTime.Now; //Save Order storeDB.Orders.Add(order); storeDB.SaveChanges(); //Process the order var cart = ShoppingCart.GetCart(this.HttpContext); cart.CreateOrder(order); return(RedirectToAction("Complete", new { id = order.OrderId })); } } catch { //Invalid - redisplay with errors return(View(order)); } }
public ActionResult Create([Bind(Include = "OrderId,OrderDate,Username,FirstName,LastName,Address,City,State,PostalCode,Country,Phone,Email,Total")] Order order) { try { if (ModelState.IsValid) { db.Orders.Add(order); db.SaveChanges(); return(RedirectToAction("Index")); } } catch { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator."); } return(View(order)); }
// // GET: /ShoppingCart/AddToCart/5 public ActionResult AddToCart(int id) { // Retrieve the album from the database var addedAlbum = storeDB.Albums .Single(album => album.AlbumId == id); // Add it to the shopping cart var cart = ShoppingCart.GetCart(storeDB, this.HttpContext); cart.AddToCart(addedAlbum); storeDB.SaveChanges(); // Go back to the main store page for more shopping return(RedirectToAction("Index")); }
public ActionResult DeleteConfirmed(int id) { Album album = db.Albums.Find(id); db.Albums.Remove(album); db.SaveChanges(); return(RedirectToAction("Search")); }
public ActionResult Create([Bind(Include = "ArtistId,Name")] Artist artist) { try { if (ModelState.IsValid) { db.Artists.Add(artist); db.SaveChanges(); return(RedirectToAction("Index")); } } catch (DataException) { ModelState.AddModelError("", "Operation failed. Please contact your system administrator if the problem persists."); } return(View(artist)); }
public ActionResult Create([Bind(Include = "OrderId,OrderDate,Username,FirstName,LastName,Address,City,State,PostalCode,Country,Phone,Email,Total")] Order order) { try { if (ModelState.IsValid) { db.Orders.Add(order); db.SaveChanges(); return(RedirectToAction("Index")); } } catch (DataException) { ModelState.AddModelError("", "Operation failed. Please contact your system administrator if the problem persists."); } return(View(order)); }
public ActionResult Create([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album) { if (album == null) { throw new System.ArgumentNullException(nameof(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)); }
public ActionResult Create([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album) { try { if (ModelState.IsValid) { db.Albums.Add(album); db.SaveChanges(); return(RedirectToAction("Index")); } } catch { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator."); } 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 Edit(Album album) { //happy path:当记录状态/模型状态为:有效时 //sad path:当模型状态:无效时执行当操作和采用路径 if (ModelState.IsValid) { //告诉数据上下文,对象在DB已经存在,采用修改模式 db.Entry(album).State = EntityState.Modified; album.Genre = db.Genres.Where(x => x.GenreId == album.GenreId).FirstOrDefault(); album.Artist = db.Artists.Where(x => x.ArtistId == album.ArtistId).FirstOrDefault(); //在数据上下文找那个通过SaveChange方法生成SQL update 命令完成对应记录的更新操作 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) { try { 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); } catch (DataException) { ModelState.AddModelError("", "Operation failed. Please contact your system administrator if the problem persists."); } return(View(album)); }
public ActionResult Index() { ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application."; var album1 = _db.Albums.First(); var artists = _db.Artists.ToList(); _db.Students.Add(new Student() { Id = 1, Name = "kumanan" }); _db.SaveChanges(); return(View()); }
// PUT api/<controller>/5 public HttpResponseMessage Put(int id, [FromBody] Album value) { MusicStoreDB.Entry(value).State = EntityState.Modified; return(ToJson(MusicStoreDB.SaveChanges())); }
// DELETE api/<controller>/5 public HttpResponseMessage Delete(int id) { MusicStoreDB.Albums.Remove(MusicStoreDB.Albums.FirstOrDefault(x => x.AlbumId == id)); return(ToJson(MusicStoreDB.SaveChanges())); }
// POST api/<controller> public HttpResponseMessage Post([FromBody] Album value) { MusicStoreDB.Albums.Add(value); return(ToJson(MusicStoreDB.SaveChanges())); }