public ActionResult Create(BangSanPham model)
        {
            CheckBangSanPham(model);
            if (ModelState.IsValid)
            {
                using (var scope = new TransactionScope())
                {
                    db.BangSanPhams.Add(model);
                    db.SaveChanges();

                    if (Request.Files["HinhAnh"] != null &&
                        Request.Files["HinhAnh"].ContentLength > 0)
                    {
                        var path = Server.MapPath("~/App_Data");
                        path = System.IO.Path.Combine(path, model.id.ToString());
                        Request.Files["HinhAnh"].SaveAs(path);

                        scope.Complete();
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        ModelState.AddModelError("HinhAnh", "Chua chon hinh anh cho san pham");
                    }
                }
            }

            ViewBag.Loai_id = new SelectList(db.LoaiSanPhams, "id", "TenLoai", model.Loai_id);
            return(View(model));
        }
예제 #2
0
        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(); // approve for transaction
                        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 Create(Product model, HttpPostedFileBase picture)
        {
            ValidateProduct(model);
            if (ModelState.IsValid)
            {
                if (picture != null)
                {
                    using (var scope = new TransactionScope())
                    {
                        db.Products.Add(model);
                        db.SaveChanges();

                        // store picture
                        var path = Server.MapPath(PICTURE_PATH);
                        picture.SaveAs(path + model.id);

                        scope.Complete();
                        return(RedirectToAction("Index"));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Picture not found!");
                }
            }

            return(View(model));
        }
예제 #4
0
        public ActionResult Create2()
        {
            using (var scope = new TransactionScope())
                try
                {
                    var bangHoaDon = Session["BangHoaDon"] as BangHoaDon;
                    var CTHoaDon   = Session["CTHoaDon"] as List <ChiTietHD>;

                    db.BangHoaDons.Add(bangHoaDon);
                    db.SaveChanges();

                    foreach (var chiTiet in CTHoaDon)
                    {
                        chiTiet.HD_id       = bangHoaDon.id;
                        chiTiet.BangSanPham = null;
                        db.ChiTietHDs.Add(chiTiet);
                    }
                    db.SaveChanges();
                    scope.Complete();

                    Session["BangHoaDon"] = null;
                    Session["CTHoaDon"]   = null;
                    return(RedirectToAction("Index"));
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", e.Message);
                }
            return(View("Create"));
        }
        public ActionResult Create(Bill model)
        {
            ValidateBill(model);
            if (ModelState.IsValid)
            {
                using (var scope = new TransactionScope())
                {
                    model.Date = DateTime.Now;
                    db.Bills.Add(model);
                    db.SaveChanges();

                    foreach (var item in ShoppingCart)
                    {
                        db.BillDetails.Add(new BillDetail
                        {
                            Bill_id    = model.id,
                            Product_id = item.Product.id,
                            Price      = item.Product.Price,
                            Quantity   = item.Quantity
                        });
                    }
                    db.SaveChanges();

                    scope.Complete();
                    Session["ShoppingCart"] = null;
                    return(RedirectToAction("Index2", "Products"));
                }
            }
            GetShoppingCart();
            ViewBag.Cart = ShoppingCart;
            return(View(model));
        }
        public ActionResult Create(string roleId, string userId)
        {
            var role = db.AspNetRoles.Find(roleId);
            var user = db.AspNetUsers.Find(userId);

            role.AspNetUsers.Add(user);
            db.Entry(role).State = EntityState.Modified;
            db.SaveChanges();

            return(RedirectToAction("Index", "AspNetRoles"));
        }
        public ActionResult Create([Bind(Include = "id,TenLoai")] LoaiSanPham loaisanpham)
        {
            if (ModelState.IsValid)
            {
                db.LoaiSanPhams.Add(loaisanpham);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(loaisanpham));
        }
        public ActionResult Create(AspNetRole aspNetRole)
        {
            if (ModelState.IsValid)
            {
                aspNetRole.Id = Guid.NewGuid().ToString();
                db.AspNetRoles.Add(aspNetRole);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(aspNetRole));
        }
예제 #9
0
 public ActionResult Edit([Bind(Include = "id,HD_id,SP_id,DonGiaBan,SoLuong")] ChiTietHD chitiethd)
 {
     if (ModelState.IsValid)
     {
         db.Entry(chitiethd).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.HD_id = new SelectList(db.BangHoaDons, "id", "TenKhachHang", chitiethd.HD_id);
     ViewBag.SP_id = new SelectList(db.BangSanPhams, "id", "MaSP", chitiethd.SP_id);
     return(View(chitiethd));
 }
예제 #10
0
 public ActionResult Create(BubleTea model)
 {
     ValidateBubleTea(model);
     if (ModelState.IsValid)
     {
         using (var scope = new TransactionScope())
         {
             // add model to database
             db.BubleTeas.Add(model);
             db.SaveChanges();
             // save file to appdata
             var path = Server.MapPath("~/App_Data");
             path = System.IO.Path.Combine(path, model.id.ToString());
             Request.Files["Image"].SaveAs(path);
             //all done successfully
             scope.Complete();
             return(RedirectToAction("Index"));
         }
     }
     return(View(model));
 }