public ActionResult Create(FormCollection _form, KT_DONGSUA_TAUTHUYEN _obj) { List <string> lstKeytxtNAM = _form.AllKeys.ToList().Where(s => s.StartsWith("txtNAM_")).ToList <String>(); List <int> lstInt = new List <int>(); lstKeytxtNAM.ForEach(o => lstInt.Add(int.Parse(o.Split('_')[o.Split('_').Count() - 1]))); //var errors = from modelstate in ModelState.AsQueryable().Where(f => f.Value.Errors.Count > 0) select new { Title = modelstate.Key }; //var a = ModelState.IsValidField("TRONG_TAI_TOIDA_COTHE"); //FDB.Common.Helpers.GetValueForm<KT_DONGSUA_TAUTHUYEN>(_form, ref _obj, "TRONG_TAI_TOIDA_COTHE"); if (ModelState.IsValid)//|| (!ModelState.IsValid && errors.ToList().Count==1 && a == false)) { _obj.NGUOI_NHAP = User.Identity.Name; _obj.NGAY_NHAP = DateTime.Now; //Save Header _context.KT_DONGSUA_TAUTHUYEN.Add(_obj); _context.SaveChanges(); int Id = _obj.ID; if (lstKeytxtNAM != null) { for (int i = 0; i < lstInt.Count; i++) { KT_DONGSUA_TAUTHUYEN_DETAIL _objDetail = new Models.KT_DONGSUA_TAUTHUYEN_DETAIL(); FDB.Common.Helpers.GetValueForm <KT_DONGSUA_TAUTHUYEN_DETAIL>(_form, lstInt[i], ref _objDetail); _objDetail.ID_DONGSUA_TAUTHUYEN = Id; _context.KT_DONGSUA_TAUTHUYEN_DETAIL.Add(_objDetail); } //Save detail: _context.SaveChanges(); } this.Information(string.Format(FDB.Common.Constants.NOTIFY_ADD_SUCCESS, "Đóng sửa tàu thuyền")); return(RedirectToAction("Index")); } else { ApplicationUser curUser = this.getCurrentUser(); var tinh = _context.DTINHTP.Where(u => (curUser.MA_TINHTP == null || curUser.MA_TINHTP.StartsWith("Z")) || u.MA_TINHTP == curUser.MA_TINHTP) .Except(_context.DTINHTP.Where(d => d.MA_TINHTP.StartsWith("Z"))); ViewBag.DM_DonVis = new SelectList(tinh, "MA_TINHTP", "TEN_TINHTP"); var quan = _context.DQUANHUYEN.Where(u => (curUser.MA_TINHTP == null || curUser.MA_TINHTP.StartsWith("Z")) || u.MA_TINHTP == curUser.MA_TINHTP); // quan.Insert(0, new DQUANHUYEN()); ViewBag.DMQhuyen = new SelectList(_context.DQUANHUYEN, "MA_QUANHUYEN", "TEN_QUANHUYEN"); ViewBag.DMPhuongXa = new SelectList(_context.DPHUONGXA, "MA_PHUONGXA", "TEN_PHUONGXA"); ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL; TempData["_SUCCESS"] = ""; //build html : int maxID = 0; String strHTML = this.GenderHTML(lstInt, _form, ref maxID); ViewBag.AddHTML = strHTML; ViewBag.sMaxID = maxID + 1; return(View(_obj)); } }
public ActionResult Delete(int id) { //Xóa header KT_DONGSUA_TAUTHUYEN _obj = _context.KT_DONGSUA_TAUTHUYEN.Find(id); _context.KT_DONGSUA_TAUTHUYEN.Remove(_obj); _context.KT_DONGSUA_TAUTHUYEN_DETAIL.Where(o => o.ID_DONGSUA_TAUTHUYEN == _obj.ID).ToList().ForEach(o => _context.KT_DONGSUA_TAUTHUYEN_DETAIL.Remove(o)); //Update thay đổi vào DB _context.SaveChanges(); this.Information(string.Format(FDB.Common.Constants.NOTIFY_DELETE_SUCCESS, "Đóng sửa tàu thuyền")); return(RedirectToAction("Index")); }
//async Task<ActionResult> public ActionResult Edit(FormCollection _form, KT_DONGSUA_TAUTHUYEN _obj) { List <string> lstKeytxtNAM = _form.AllKeys.ToList().Where(s => s.StartsWith("txtNAM_")).ToList <String>(); List <int> lstInt = new List <int>(); lstKeytxtNAM.ForEach(o => lstInt.Add(int.Parse(o.Split('_')[o.Split('_').Count() - 1]))); //var errors = from modelstate in ModelState.AsQueryable().Where(f => f.Value.Errors.Count > 0) select new { Title = modelstate.Key }; //var a = ModelState.IsValidField("TRONG_TAI_TOIDA_COTHE"); //FDB.Common.Helpers.GetValueForm<KT_DONGSUA_TAUTHUYEN>(_form, ref _obj, "TRONG_TAI_TOIDA_COTHE"); if (ModelState.IsValid) //|| (!ModelState.IsValid && errors.ToList().Count==1 && a == false)) { var model = _context.KT_DONGSUA_TAUTHUYEN.First(o => o.ID == _obj.ID); FDB.Common.Helpers.CopyObject <KT_DONGSUA_TAUTHUYEN>(_obj, ref model); model.NGUOI_NHAP = User.Identity.Name; model.NGAY_NHAP = DateTime.Now; var dbEntityEntry = _context.Entry(model); _context.KT_DONGSUA_TAUTHUYEN.Attach(model); _context.Entry(model).State = System.Data.Entity.EntityState.Modified; //Xóa những detail cũ: _context.KT_DONGSUA_TAUTHUYEN_DETAIL.Where(o => o.ID_DONGSUA_TAUTHUYEN == _obj.ID).ToList().ForEach(o => _context.KT_DONGSUA_TAUTHUYEN_DETAIL.Remove(o)); //Thêm mới detail đã sửa int Id = _obj.ID; if (lstKeytxtNAM != null) { for (int i = 0; i < lstInt.Count; i++) { KT_DONGSUA_TAUTHUYEN_DETAIL _objDetail = new Models.KT_DONGSUA_TAUTHUYEN_DETAIL(); FDB.Common.Helpers.GetValueForm <KT_DONGSUA_TAUTHUYEN_DETAIL>(_form, lstInt[i], ref _objDetail); _objDetail.ID_DONGSUA_TAUTHUYEN = Id; _context.KT_DONGSUA_TAUTHUYEN_DETAIL.Add(_objDetail); } } //Save data: _context.SaveChanges(); this.Information(string.Format(FDB.Common.Constants.NOTIFY_UPDATE_SUCCESS, "Đóng sửa tàu thuyền")); return(RedirectToAction("Index")); } else { TempData["_SUCCESS"] = ""; ApplicationUser curUser = this.getCurrentUser(); var tinh = _context.DTINHTP.Where(u => (curUser.MA_TINHTP == null || curUser.MA_TINHTP.StartsWith("Z")) || u.MA_TINHTP == curUser.MA_TINHTP) .Except(_context.DTINHTP.Where(d => d.MA_TINHTP.StartsWith("Z"))); ViewBag.DM_DonVis = new SelectList(tinh, "MA_TINHTP", "TEN_TINHTP"); var quan = _context.DQUANHUYEN.Where(u => u.MA_TINHTP == _obj.MA_TINHTP); // quan.Insert(0, new DQUANHUYEN()); ViewBag.DMQhuyen = new SelectList(quan, "MA_QUANHUYEN", "TEN_QUANHUYEN"); var phuongxa = _context.DPHUONGXA.Where(u => _obj.MA_QUANHUYEN == null || u.MA_QUANHUYEN == _obj.MA_QUANHUYEN); ViewBag.DMPhuongXa = new SelectList(phuongxa, "MA_PHUONGXA", "TEN_PHUONGXA"); _obj.DSDongSuaTauThuyenDetail = new List <KT_DONGSUA_TAUTHUYEN_DETAIL>(); //build html : int maxID = 0; String strHTML = this.GenderHTML(lstInt, _form, ref maxID); ViewBag.sEditHTML = strHTML; ViewBag.MaxID = maxID + 1; return(View(_obj)); } }