public ActionResult Edit(BangSanPham model) { CheckBangSanPham(model); if (ModelState.IsValid) { using (var scope = new TransactionScope()) { db.Entry(model).State = EntityState.Modified; db.SaveChanges(); var path = Server.MapPath("~/App_Data"); path = path + "/" + model.id; if (Request.Files["HinhAnh"] != null && Request.Files["HinhAnh"].ContentLength > 0) { Request.Files["HinhAnh"].SaveAs(path); } scope.Complete(); // approve for transaction return(RedirectToAction("Index")); } } ViewBag.Loai_id = new SelectList(db.LoaiSanPhams, "id", "TenLoai", model.Loai_id); return(View(model)); }
public ActionResult Create(BangSanPham model) { CheckBangSanPham(model); if (ModelState.IsValid) { using (var scope = new TransactionScope()) { db.BangSanPhams.Add(model); db.SaveChanges(); var path = Server.MapPath("~/App_Data"); path = path + "/" + model.id; if (Request.Files["HinhAnh"] != null && Request.Files["HinhAnh"].ContentLength > 0) { Request.Files["HinhAnh"].SaveAs(path); scope.Complete(); return(RedirectToAction("Index")); } else { ModelState.AddModelError("HinhAnh", "Chưa có hình sản phẩm!"); } } } ViewBag.Loai_id = new SelectList(db.LoaiSanPhams, "id", "TenLoai", model.Loai_id); return(View(model)); }
public ActionResult DeleteConfirmed(int id) { BangSanPham bangsanpham = db.BangSanPhams.Find(id); db.BangSanPhams.Remove(bangsanpham); db.SaveChanges(); return(RedirectToAction("Index")); }
// GET: /DanhSachSanPham/Edit/5 public ActionResult Edit(int id) { BangSanPham model = db.BangSanPhams.Find(id); if (model == null) { return(HttpNotFound()); } ViewBag.Loai_id = new SelectList(db.LoaiSanPhams, "id", "TenLoai", model.Loai_id); return(View(model)); }
// GET: /BangSanPham/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } BangSanPham bangsanpham = db.BangSanPhams.Find(id); if (bangsanpham == null) { return(HttpNotFound()); } return(View(bangsanpham)); }
// GET: /BangSanPham/Edit/5 public ActionResult Edit(int id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } BangSanPham bangsanpham = db.BangSanPhams.Find(id); if (bangsanpham == null) { return(HttpNotFound()); } ViewBag.Loai_id = new SelectList(db.LoaiSanPhams, "id", "TenLoai", bangsanpham.Loai_id); return(View(bangsanpham)); }
private void CheckBangSanPham(BangSanPham model) { if (model.GiaGoc < 0) { ModelState.AddModelError("GiaGoc", "Giá gốc phải lớn hơn 0!"); } if (model.GiaBan < model.GiaGoc) { ModelState.AddModelError("GiaBan", "Giá bán phải lớn hơn giá gốc!"); } if (model.GiaGop < model.GiaGoc) { ModelState.AddModelError("GiaGop", "Giá góp phải lớn hơn giá gốc!"); } }
private void CheckBangSanPham(BangSanPham model) { if (model.GiaGoc < 0) { ModelState.AddModelError("GiaGoc", "Gia goc phai lon hon 0"); } if (model.GiaGoc > model.GiaBan) { ModelState.AddModelError("GiaBan", "Gia ban phai lon hon gia goc"); } if (model.GiaGoc > model.GiaGop) { ModelState.AddModelError("GiaGop", "Gia gop phai lon hon gia goc"); } if (model.SoLuongTon < 0) { ModelState.AddModelError("SoLuongTon", "So luong ton phai lon hon 0"); } }
public void TestCreate2() { var controller = new BangSanPhamController(); var context = new Mock <HttpContextBase>(); var request = new Mock <HttpRequestBase>(); var files = new Mock <HttpFileCollectionBase>(); var file = new Mock <HttpPostedFileBase>(); context.Setup(c => c.Request).Returns(request.Object); request.Setup(r => r.Files).Returns(files.Object); files.Setup(f => f["HinhAnh"]).Returns(file.Object); file.Setup(f => f.ContentLength).Returns(1); context.Setup(c => c.Server.MapPath("~/App_Data")).Returns("~/App_Data"); controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller); var db = new CS4PEntities(); var model = new BangSanPham(); model.Loai_id = db.LoaiSanPhams.First().id; model.TenSP = "TenSP"; model.MaSP = "MaSP"; model.GiaGoc = 123; model.GiaBan = 456; model.GiaGop = 789; model.SoLuongTon = 10; using (var scope = new TransactionScope()) { var result0 = controller.Create(model) as RedirectToRouteResult; Assert.IsNotNull(result0); file.Verify(f => f.SaveAs(It.Is <string>(s => s.StartsWith("~/App_Data/")))); Assert.AreEqual("Index", result0.RouteValues["action"]); file.Setup(f => f.ContentLength).Returns(0); var result1 = controller.Create(model) as ViewResult; Assert.IsNotNull(result1); Assert.IsInstanceOfType(result1.ViewData["Loai_id"], typeof(SelectList)); } }