Example #1
0
        public ActionResult Create([Bind(Include = "CategoryId,ItemName,ItemInfo,IsActive,ItemStatus")] Item item, Price price)
        {
            HttpPostedFileBase thumb = Request.Files["imageUpload"];
            if (ModelState.IsValid)
            {
                if (thumb != null && thumb.ContentLength > 0)
                {
                    string pathToSave = Server.MapPath("~/Images/Uploads");
                    string fileName = thumb.FileName;
                    thumb.SaveAs(Path.Combine(pathToSave, fileName));
                    item.ThumbnailPath = "/Images/Uploads/" + fileName;
                }
                try
                {
                    //Add Item into Database
                    db.Items.Add(item);
                    db.SaveChanges();
                }
                catch (Exception)
                {
                    return View(item);
                }

                //add price into database
                //Lấy Id của item mới tạo

                price.ItemId = db.Items.OrderByDescending(i => i.ItemId).FirstOrDefault().ItemId;
                price.DateStart = DateTime.Now;
                db.Prices.Add(price);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(item);
        }
Example #2
0
        public ActionResult Edit([Bind(Include = "ItemId,CategoryId,ItemName,ItemInfo,IsActive,ItemStatus,IsDelete")] Item itemInput, Price price, string hf_ThumbnailPath)
        {
            HttpPostedFileBase thumb = Request.Files["imageUpload"];
            if (ModelState.IsValid)
            {
                //edit thumbnail
                //Nếu có chọn ảnh đại diện mới
                if (thumb != null && thumb.ContentLength > 0)
                {
                    //Xóa ảnh cũ
                    string fullPath = Request.MapPath("~" + hf_ThumbnailPath);
                    if (System.IO.File.Exists(fullPath))
                    {
                        System.IO.File.Delete(fullPath);
                    }
                    //lưu ảnh mới
                    string pathToSave = Server.MapPath("~/Images/Uploads");
                    string fileName = thumb.FileName;
                    thumb.SaveAs(Path.Combine(pathToSave, fileName));
                    itemInput.ThumbnailPath = "/Images/Uploads/" + fileName;
                }
                else
                {

                    itemInput.ThumbnailPath = hf_ThumbnailPath;
                }
                Item it = db.Items.Find(itemInput.ItemId);
                it.CategoryId = itemInput.CategoryId;
                it.ItemName = itemInput.ItemName;
                it.ItemInfo = itemInput.ItemInfo;
                it.IsActive = itemInput.IsActive;
                it.IsDelete = itemInput.IsDelete;
                it.ThumbnailPath = itemInput.ThumbnailPath;

                db.SaveChanges();
                //edit price
                //Lấy list Price theo: Item id, chưa bị delete
                Price pri = db.Prices.Where(p => p.ItemId == itemInput.ItemId & p.IsDelete != true).FirstOrDefault();
                if (pri != null) //neu co gia truoc do roi thi
                {
                    //Nếu giá cũ = giá mới => không thay đổi.
                    //Nếu giá cũ!= giá mới => xóa tất cả giá cũ, chèn giá mới.
                    if (pri.UnitPrice != price.UnitPrice)
                    {
                        pri.IsDelete = true;
                    }
                }
                price.DateStart = DateTime.Now;
                db.Prices.Add(price);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            return View(itemInput);
        }