public static CarTypeViewModel CreateCarTypeViewModel(CarType carType) { ApplicationDbContext db = new ApplicationDbContext(); CarTypeViewModel ectvm = new CarTypeViewModel(); ectvm.CarTypeData = carType; ectvm.CarEdited = new Car();//Just to prevent it being null //ectvm.CarModels = new SelectList(db.CarModels, "CarModelId", "Description", ectvm.CarTypeData.CarModelId); return ectvm; }
public ActionResult Create(CarTypeViewModel ctvm) { if (ModelState.IsValid) { db.CarTypes.Add(ctvm.CarTypeData); db.SaveChanges(); TempData["Added"] = ctvm.CarTypeData.Description + " Added"; return RedirectToAction("Index"); } ctvm = ViewModelFactory.CreateCarTypeViewModel(); return View(ctvm); }
//public ActionResult Edit([Bind(Include = "CarTypeId,CarCode,CarModelId,DailyPrice,DailyLatePrice,Gear")] CarType carType) public ActionResult Edit(CarTypeViewModel ctvm) { if (ModelState.IsValid) { db.Entry(ctvm.CarTypeData).State = EntityState.Modified; db.SaveChanges(); TempData["Added"] = ctvm.CarTypeData.Description + " Edited"; return RedirectToAction("Index"); } ctvm = ViewModelFactory.CreateCarTypeViewModel(ctvm.CarTypeData); return View(ctvm); }