// GET: Demands/Edit/5 public ActionResult Edit(int id, int SLid) { if (id == null || SLid == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Demand demand = db.Demands.Find(id); if (demand == null) { return(HttpNotFound()); } var Dmds = db.DemandDetails.FirstOrDefault(d => d.DemandID == id && d.SubLedgerID == SLid); int pd = GetCurrentPeriod(); var dp = db.DemandPeriods.FirstOrDefault(p => p.DDID == Dmds.DDID && p.PeriodID == pd); var DME = new DemandInitEdit { DemandID = demand.DemandID, DDID = Dmds.DDID, HouseNo = demand.HouseNo, Remarks = demand.Remarks, StopDate = demand.StopDate.Year, Amt = dp.Amount, DemandPeriodID = dp.PeriodID }; ViewBag.Citizen = demand.Citizen.Name; ViewBag.CitizenID = demand.CitizenID; ViewBag.SubLedger = $"For: {Dmds.SubLedger.Ledger1.Ledger1 } >> {Dmds.SubLedger.Ledger}"; ViewBag.YearBox = MyExtensions.MakeYrRq(1, 1, DateTime.Today.Year); return(View(DME)); }
public ActionResult Edit([Bind(Include = "DemandID, DDID, HouseNo, Amt, Remarks, StopDate, DemandPeriodID")] DemandInitEdit dmd, FormCollection fm) { if (ModelState.IsValid) { //Save Demand record House No var edm = db.Demands.Find(dmd.DemandID); edm.HouseNo = dmd.HouseNo; edm.StopDate = DateTime.Parse("31/March/" + dmd.StopDate); edm.Remarks = dmd.Remarks; db.Entry(edm).State = EntityState.Modified; //Save demand period Details record var edmd = db.DemandPeriods.Find(dmd.DDID, dmd.DemandPeriodID); edmd.Amount = dmd.Amt; db.Entry(edmd).State = EntityState.Modified; foreach (var item in fm) { if (item.ToString().Length > 5 && item.ToString().Substring(0, 5) == "data-") { int iLedDetID = int.Parse(item.ToString().Substring(item.ToString().LastIndexOf('-') + 1)); var cld = db.DemandLedgerDetails.Find(iLedDetID); cld.DemandLedgerDetail1 = fm[item.ToString()]; db.Entry(cld).State = EntityState.Modified; } } try { // Your code... // Could also be before try if you know the exception occurs in SaveChanges db.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } return(RedirectToAction("Details", "Citizens", new { id = edm.CitizenID })); } return(RedirectToAction("Create")); }