public ActionResult Create(Walk walk) //Method overloading, two identical methods with different parameters { try { walk.Duration = walk.Duration * 60; _walkRepo.AddWalk(walk); return(RedirectToAction("Index")); } catch (Exception ex) { int ownerId = GetCurrentUserId(); Owner owner = _ownerRepo.GetOwnerById(ownerId); List <Walker> walkers = _walkerRepo.GetWalkersInNeighborhood(owner.NeighborhoodId); List <Dog> dogs = _dogRepo.GetDogsByOwnerId(ownerId); WalkFormViewModel vm = new WalkFormViewModel() { Walk = new Walk(), Walkers = walkers, Dogs = dogs }; return(View(vm)); } }
//this is the POST functionality in the Create method for a walk, which will take in the selectedDogs Ids as strings and the view model public ActionResult Create(IEnumerable <string> selectedDogs, WalkFormViewModel walkform) { try { foreach (string idString in selectedDogs) { walkform.Walk.DogId = int.Parse(idString); _walkRepo.AddWalk(walkform.Walk); } return(RedirectToAction(nameof(Index))); } catch { //if something goes wrong we return to the view, which is the WalkFormViewModel List <Walker> walkers = _walkerRepo.GetAllWalkers(); List <Dog> dogs = _dogRepo.GetAllDogs(); List <SelectListItem> listSelectListItem = new List <SelectListItem>(); WalkFormViewModel vm = new WalkFormViewModel() { Walk = new Walk(), Dogs = dogs, Walkers = walkers, DogsList = listSelectListItem }; return(View(vm)); } }
public ActionResult Create(Walk walk) { try { _walkRepo.AddWalk(walk); return(RedirectToAction(nameof(Index))); } catch { return(View(walk)); } }
public ActionResult Create(WalksFormViewModel vm) { try { Walker walker = _walkerRepo.GetWalkerById(vm.Walk.WalkerId); vm.Walk.Walker = walker; vm.Walk.Duration = vm.Walk.Duration * 60; _walkRepo.AddWalk(vm.Walk); return(RedirectToAction("Index", "Owner")); } catch { return(View(vm)); } }
public ActionResult Create(RequestWalkViewModel viewModel) { try { _walkRepo.AddWalk(viewModel.Walk); return(RedirectToAction(nameof(Index), "Owners")); } catch { RequestWalkViewModel sameView = viewModel; viewModel.Dogs = _dogRepo.GetDogsByOwnerId(GetCurrentUserId()); return(View(sameView)); } }
public ActionResult CreateWalk(WalkFormViewModel walkFormViewModel) { try { foreach (int dogId in walkFormViewModel.DogIds) { Walk walk = new Walk() { Date = walkFormViewModel.Walk.Date, Duration = walkFormViewModel.Walk.Duration, WalkerId = walkFormViewModel.Walk.WalkerId, DogId = dogId }; _walkRepo.AddWalk(walk); } return(RedirectToAction("Index")); } catch { return(View(walkFormViewModel)); } }