public async Task<ActionResult> New() { // Default to California, USA var model = new PersonEditorModel { SelectedRegionDdl = "CA", Country = "US" }; await GetCountryAndRegionData(model); return View(model); }
public async Task<ActionResult> New(PersonEditorModel model) { if (ModelState.IsValid) { var person = Mapper.Map<PersonEditorModel, Person>(model); person.Id = Guid.NewGuid(); Context.People.Add(person); await Context.SaveChangesAsync(); return RedirectToAction("Index", "Home"); } await GetCountryAndRegionData(model); return View(model); }
public async Task<ActionResult> Edit(PersonEditorModel model) { if (ModelState.IsValid) { var person = await GetPersonAsync(model.Id); if (person == null) { return HttpNotFound(); } Mapper.Map(model, person); await Context.SaveChangesAsync(); return RedirectToAction("Index", "Home"); } await GetCountryAndRegionData(model); return View(model); }
private async Task GetCountryAndRegionData(PersonEditorModel model) { var countries = await Context.Countries .OrderBy(c => c.Name) .ToListAsync() .ConfigureAwait(false); model.Countries.AddRange(Mapper.Map<List<SelectListItem>>(countries)); var regions = await Context.Regions.ToListAsync(); model.RegionsByCountry = regions .GroupBy(kvp => kvp.CountryId) .ToDictionary( grp => grp.Key, grp => grp.OrderBy(r => r.Name).Select(Mapper.Map<Region, SelectListItem>).ToList() ); }