public ActionResult EditCar(CarViewModel viewModel) { if (LoginUserSession.Current.IsAuthenticated) { if (viewModel == null) { TempData["Message"] = "No View Model"; return(RedirectToAction("Edit")); } CarsRepository repo = new CarsRepository(); Car car = repo.GetByID(viewModel.ID); if (car == null) { car = new Car(); } car.Model = viewModel.Model; car.Brand = viewModel.Brand; car.Color = viewModel.Color; car.HP = viewModel.HP; repo.Save(car); TempData["Mesage"] = "Car was successfully saved!"; return(RedirectToAction("Edit")); } TempData["ErrorMessage"] = "Please log in"; return(RedirectToAction("Login", "Login")); }
public ActionResult EditCar(int id = 0) { if (!LoginUserSession.Current.IsAdministrator) { return(RedirectToAction("Edit")); } BrandRepository brandRepo = new BrandRepository(); List <Brand> brands = brandRepo.GetAll(); Brand brand = new Brand(); ViewBag.Brands = new SelectList(brands, "ID", "Name"); CarsRepository carRepo = new CarsRepository(); CarViewModel car; if (id != 0) { car = new CarViewModel(carRepo.GetByID(id)); } else { car = new CarViewModel(new Car()); } return(View(car)); }
public ActionResult Edit(int id) { var dbCar = repo.GetByID(id); if (dbCar.Owner != LoginSession.Current.UserID) { return(RedirectToAction("Car", "UserCars")); } var car = new CarViewModel(); car.ID = id; var models = modelsRepo.GetAll(); ViewBag.Models = new SelectList(models, "ID", "Name"); return(View(car)); }
public ActionResult Delete(int id = 0) { if (!LoginUserSession.Current.IsAdministrator) { return(Edit()); } CarsRepository repo = new CarsRepository(); if (repo.GetByID(id) != null) { repo.DeleteByID(id); TempData["Message"] = "Successfully deleted car!"; } else { TempData["ErrorMessage"] = "No such car!"; } return(RedirectToAction("Edit")); }