public ActionResult Create(CarModelViewModel model) { try { if (!ModelState.IsValid) { return(View(model)); } // Checks if car model already exists: if (logic.IsCarModelExists(model.CarModel)) { ViewBag.ErrorMessage = "Car model already exists."; return(View(model)); } else { // Car model is OK to insert: logic.InsertCarModel(model.CarModel); return(RedirectToAction("Index")); } } catch (Exception) { ViewBag.ErrorMessage = "An error has occurred. please try again later."; return(View(model)); } }
public ActionResult Edit(CarModelViewModel model) { try { if (!ModelState.IsValid) { return(View(model)); } logic.UpdateCarModel(model.CarModel); return(RedirectToAction("Index")); } catch (Exception) { ViewBag.ErrorMessage = "An error has occurred. please try again later."; return(View(model)); } }
/// <summary> /// Page displays: A form to edit a car model. /// </summary> public ActionResult Edit(int id = 0) { try { // Gets the car model by the ID: CarModel carModel = logic.GetCarModelByID(id); if (carModel == null) { return(HttpNotFound()); } // Creates the model object: CarModelViewModel model = new CarModelViewModel(); model.CarModel = carModel; return(View(model)); } catch (Exception) { ViewBag.ErrorMessage = "An error has occurred. please try again later."; return(View(new CarModelViewModel())); } }