public PersonViewModel GetPersonViewModel(int id) { var person = this.GetPerson(id); if (person == null) { return null; } var personViewModel = new PersonViewModel() { PersonId = person.PersonId, FirstName = person.FirstName, LastName = person.LastName, IsAuthorised = person.IsAuthorised, IsEnabled = person.IsEnabled, IsValid = person.IsValid, Colours = new List<ColourViewModel>() }; var allColours = _context.Colours.OrderBy(c => c.Name); var allColourViewModels = Mapper.Map<List<ColourViewModel>>(allColours); var favouriteColourIds = person.Colours.Select(c => c.ColourId); personViewModel = LoadColourViewModels(personViewModel, favouriteColourIds, allColourViewModels); return personViewModel; }
public ActionResult Edit(PersonViewModel personViewModel) { if (ModelState.IsValid) { UpdatePerson(personViewModel); return RedirectToAction("Index"); } return View(personViewModel); }
public HttpResponseMessage PutPersonViewModel(int id, PersonViewModel personViewModel) { if (!ModelState.IsValid) { return Request.CreateResponse(HttpStatusCode.BadRequest, ModelState); } if (id != personViewModel.PersonId) { return Request.CreateResponse(HttpStatusCode.BadRequest); } var personParameterModel = Mapper.Map<PersonParameterModel>(personViewModel); _personRepository.UpdatePerson(personParameterModel); return Request.CreateResponse(HttpStatusCode.NoContent); }
private PersonViewModel LoadColourViewModels(PersonViewModel personViewModel, IEnumerable<int> favouriteColourIds, List<ColourViewModel> allColourViewModels) { for (int i = 0; i < allColourViewModels.Count; i++) { var tempColour = allColourViewModels.ElementAt(i); if (favouriteColourIds.Contains(tempColour.ColourId)) { tempColour.IsChecked = true; } personViewModel.Colours.Add(tempColour); } return personViewModel; }
private void UpdatePerson(PersonViewModel personViewModel) { var personParameterModel = Mapper.Map<PersonParameterModel>(personViewModel); _personRepository.UpdatePerson(personParameterModel); }