public ActionResult Create(Dog dog) { try { // LOOK AT THIS // Let's save a new dog // This new dog may or may not have Notes and/or an ImageUrl _dogRepository.AddDog(dog); return(RedirectToAction(nameof(Index))); } catch { // LOOK AT THIS // When something goes wrong we return to the view // BUT our view expects a DogFormViewModel object...so we'd better give it one DogFormViewModel vm = new DogFormViewModel() { Dog = dog, Owners = _ownerRepository.GetAll(), }; return(View(vm)); } }
public ActionResult Create(Dog dog) { try { //add a new dog to the database //this new dog may or may not have a value for Notes and/or ImageUrl //since we are not allowing user to chose the owner when adding a dog, we need to set the OwnerId to the id of the user/owner that is signed in; dog.OwnerId = GetCurrentUserId(); _dogRepo.AddDog(dog); return(RedirectToAction("Index")); } catch { List <Owner> owners = _ownerRepo.GetAllOwners(); //if something goes wrong we return to the view, which is the DogFormViewModel DogFormViewModel vm = new DogFormViewModel() { Dog = dog, Owners = owners }; return(View(vm)); } }
// LOOK AT THIS public ActionResult Create() { // We use a view model because we need the list of Owners in the Create view DogFormViewModel vm = new DogFormViewModel() { Dog = new Dog(), }; return(View(vm)); }
public ActionResult Create() { List <Owner> owners = _ownerRepo.GetAllOwners(); DogFormViewModel vm = new DogFormViewModel() { Dog = new Dog(), Owners = owners }; return(View(vm)); }
// GET: DogsController/Create public ActionResult Create() { List <Owner> owners = _ownerRepo.GetAllOwners(); //using view model so we can connect the list of owners with the individual dog DogFormViewModel vm = new DogFormViewModel() { Dog = new Dog(), Owners = owners }; return(View(vm)); }
public ActionResult Edit(int id, DogFormViewModel vm) { try { _dogRepo.UpdateDog(vm.Dog); return(RedirectToAction("Index")); } catch (Exception ex) { return(View(vm)); } }
public ActionResult Create(DogFormViewModel vm) { try { _dogRepo.AddDog(vm.Dog); return(RedirectToAction("Index")); } catch { return(View(vm)); } }
// GET: DogController/Edit/5 public ActionResult Edit(int id) { DogFormViewModel vm = new DogFormViewModel() { Dog = _dogRepo.GetDogById(id), Owners = _ownerRepo.GetAllOwners() }; if (vm.Dog == null) { return(NotFound()); } return(View(vm)); }
public ActionResult Edit(int id, Dog dog) { try { _dogRepo.UpdateDog(dog); return(RedirectToAction("Index")); } catch { DogFormViewModel vm = new DogFormViewModel() { Dog = dog, Owners = _ownerRepo.GetAllOwners() }; return(View(vm)); } }
// GET: DogController/Edit/5 public ActionResult Edit(int id) { Dog dog = _dogRepo.GetDogById(id); List <Owner> owners = _ownerRepo.GetAllOwners(); if (dog.OwnerId != GetCurrentUserId()) { return(NotFound()); } DogFormViewModel vm = new DogFormViewModel() { Dog = dog, Owners = owners }; if (vm == null) { return(NotFound()); } return(View(vm)); }