public ActionResult Edit([Bind(Include = "id,title,desc,price_old,price,flag")] Models.Item item_tmp, FormCollection collection) { try { if (ModelState.IsValid) { //Item var item = db.Items.Find(item_tmp.id); item.title = item_tmp.title; item.priceOld = item_tmp.priceOld; item.price = item_tmp.price; item.desc = item_tmp.desc; item.quantity = item.quantity + long.Parse(collection["quantity_add"]); item.quantityTotal = item.quantityTotal + long.Parse(collection["quantity_add"]); //Images var images = TM.IO.UploadImages(Request.Files, DirUpload.imagesProduct); var tmp = images.UploadFileString(); if (tmp != null) { item.images = tmp; } item.updatedBy = Common.Auth.getUser().id.ToString(); item.updatedAt = DateTime.Now; db.Entry(item).State = EntityState.Modified; //Group Item var group_item = db.GroupItems.Where(d => d.itemId == item.id && d.flag > 0).FirstOrDefault(); if (group_item != null) { group_item.groupId = Guid.Parse(collection["group_id"]); db.Entry(item).State = EntityState.Modified; } else { group_item = new Models.GroupItem(); group_item.id = Guid.NewGuid(); group_item.appKey = item.appKey; group_item.groupId = Guid.Parse(collection["group_id"]); group_item.itemId = item.id; group_item.flag = 1; group_item.updatedBy = Common.Auth.getUser().id.ToString(); group_item.updatedAt = DateTime.Now; db.GroupItems.Add(group_item); } db.SaveChanges(); this.success(TM.Common.Language.msgUpdateSucsess); return(RedirectToAction("Index")); } else { this.danger(TM.Common.Language.msgError); } } catch (Exception ex) { this.danger(TM.Common.Language.msgUpdateError); } return(RedirectToAction("Edit")); }
public ActionResult Create([Bind(Include = "title,code_key,desc,quantity,price_old,price,flag")] Models.Item item, FormCollection collection) { try { if (ModelState.IsValid) { if (db.Items.Any(d => d.codeKey.ToUpper() == item.codeKey.ToUpper() && d.appKey == AppKey.product)) { this.danger(TM.Common.Language.msgExistError); return(RedirectToAction("Create")); } //Item item.id = Guid.NewGuid(); item.appKey = AppKey.product; item.codeKey = item.codeKey.ToUpper(); item.quantityTotal = item.quantity; item.createdBy = Common.Auth.getUser().id.ToString(); item.createdAt = DateTime.Now; //Images var images = TM.IO.UploadImages(Request.Files, DirUpload.imagesProduct); item.images = images.UploadFileString(); db.Items.Add(item); //Group Item var group_item = new Models.GroupItem(); group_item.id = Guid.NewGuid(); group_item.appKey = item.appKey; group_item.groupId = Guid.Parse(collection["group_id"]); group_item.itemId = item.id; group_item.flag = item.flag; group_item.createdBy = Common.Auth.getUser().id.ToString(); group_item.createdAt = DateTime.Now; db.GroupItems.Add(group_item); db.SaveChanges(); } else { this.danger(TM.Common.Language.msgError); } this.success(TM.Common.Language.msgCreateSucsess); } catch (Exception ex) { this.danger(TM.Common.Language.msgCreateError); } return(RedirectToAction("Create")); }