public ActionResult Edit([Bind(Include = "LgaID,StateID,LgaName")] LgaViewModel lga) { if (lga.LgaName != null) { try { var lgaList = (from d in db.Lgas.Where(x => x.LgaID == lga.LgaID) select d).FirstOrDefault(); if (lgaList != null) { lgaList.LgaName = lga.LgaName; db.SaveChanges(); ViewBag.DisplayMessage = "success"; ModelState.AddModelError("", "Updated Successfully!"); } } catch (Exception ex) { ViewBag.DisplayMessage = "Info"; ModelState.AddModelError("", "Error: " + ex.Message); } } else { ViewBag.DisplayMessage = "Info"; ModelState.AddModelError("", "Enter Lga Name!"); } return(View(lga)); }
public ActionResult Add([Bind(Include = "LgaID,StateID,LgaName")] LgaViewModel lga) { try { var lgaAdd = new Lga { StateID = Convert.ToInt16(lga.StateID.ToString()), LgaID = Convert.ToInt16(lga.LgaID.ToString()), LgaName = lga.LgaName }; var isExist = (from l in db.Lgas.Where(x => x.LgaName == lga.LgaName) select l).FirstOrDefault(); if (isExist == null) { db.Lgas.Add(lgaAdd); db.SaveChanges(); ViewBag.DisplayMessage = "success"; ModelState.AddModelError("", " Saved Successfully!"); LoadDropDownList(); } else { ViewBag.DisplayMessage = "Info"; ModelState.AddModelError("", "LGA Name " + isExist.LgaName + " already exist, Please enter a new Lga!"); LoadDropDownList(); } } catch (Exception ex) { ViewBag.DisplayMessage = "Info"; ModelState.AddModelError("", "Error: " + ex.Message); } return(View(lga)); }