public ActionResult Create(ZoneChartVM v) { ZoneChart z = new ZoneChart(); if (v.country != null && v.StatusZone == "I") { z.ZoneCategoryID = v.ZoneCategoryID; z.ZoneID = v.ZoneID; //char []sep={','}; //string [] lst = v.countries.Split(sep); List <ZoneChartDetail> l = new List <ZoneChartDetail>(); foreach (var i in v.country) { ZoneChartDetail s = new ZoneChartDetail(); s.CountryID = Convert.ToInt32(i); l.Add(s); } z.ZoneChartDetails = l; db.ZoneCharts.Add(z); db.SaveChanges(); } if (v.StatusZone == "D" && v.city != null) { z.ZoneCategoryID = v.ZoneCategoryID; z.ZoneID = v.ZoneID; List <ZoneChartDetail> l = new List <ZoneChartDetail>(); foreach (var i in v.city) { ZoneChartDetail s = new ZoneChartDetail(); s.CountryID = Convert.ToInt32(Session["depotcountry"].ToString()); s.CityID = Convert.ToInt32(i); l.Add(s); } z.ZoneChartDetails = l; db.ZoneCharts.Add(z); db.SaveChanges(); } TempData["SuccessMsg"] = "You have successfully added Zone Chart."; return(RedirectToAction("Index")); }
public ActionResult Edit(ZoneChartVM v) { var list = db.ZoneChartDetails.Where(x => x.ZoneChartID == v.ZoneChartID).ToList(); foreach (var item in list) { db.ZoneChartDetails.Remove(item); db.SaveChanges(); } ZoneChart z = new ZoneChart(); z.ZoneChartID = v.ZoneChartID; z.ZoneCategoryID = v.ZoneCategoryID; z.ZoneID = v.ZoneID; db.Entry(z).State = EntityState.Modified; if (v.country != null && v.StatusZone == "I") { foreach (var i in v.country) { ZoneChartDetail s = new ZoneChartDetail(); s.CountryID = Convert.ToInt32(i); s.ZoneChartID = z.ZoneChartID; db.ZoneChartDetails.Add(s); db.SaveChanges(); } } if (v.StatusZone == "D" && v.city != null) { foreach (var i in v.city) { ZoneChartDetail s = new ZoneChartDetail(); s.CountryID = Convert.ToInt32(Session["depotcountry"].ToString()); s.CityID = Convert.ToInt32(i); s.ZoneChartID = z.ZoneChartID; db.ZoneChartDetails.Add(s); db.SaveChanges(); } } TempData["SuccessMsg"] = "You have successfully Updated Zone Chart."; return(RedirectToAction("Index")); }