예제 #1
0
        public ActionResult DomesticUnit(PetDomesticUnitViewModel viewModel)
        {
            PetLives relation = new PetLives
            {
                PetID          = viewModel.Pet.PetID,
                DomesticUnitID = viewModel.DomesticUnitID
            };

            relation.Pet = repository.Pets
                           .FirstOrDefault(p => p.PetID == relation.PetID);
            relation.DomesticUnit = repository.DomesticUnits
                                    .FirstOrDefault(d => d.DomesticUnitID == relation.DomesticUnitID);

            if (ModelState.IsValid)
            {
                repository.SavePetLives(relation);
                TempData["message"] = $"{relation.Pet.Name} now lives in {relation.DomesticUnit.Name}";
                return(RedirectToAction("Index"));
            }
            else
            {
                // if enters here there is something wrong with the data values
                return(View(relation));
            }
        }
예제 #2
0
        public ViewResult DomesticUnit(Guid id)
        {
            PetDomesticUnitViewModel viewModel = new PetDomesticUnitViewModel
            {
                Pet = repository.Pets.FirstOrDefault(p => p.PetID == id)
            };
            PetLives relation = repository.PetLivesTable
                                .FirstOrDefault(p => p.PetID == id);

            viewModel.DomesticUnits = relation == null ? repository.DomesticUnits : repository.DomesticUnits.Where(d => d.DomesticUnitID != relation.DomesticUnitID);

            if (relation != null)
            {
                DomesticUnit domesticUnit = repository.DomesticUnits
                                            .FirstOrDefault(d => d.DomesticUnitID == relation.DomesticUnitID);
                viewModel.CurrentDomesticUnitName = domesticUnit.Name;
            }
            return(View(viewModel));
        }