public ActionResult CreateMultipleCities(int?id) { if (id is null) { return(View("Error")); } var state = db.States.FirstOrDefault(m => m.Id == id); if (state is null) { return(View("Error")); } var model = new AdminMarketsPlaces_CreateMultipleCitiesViewModel { Delimiters = new Dictionary <string, int>(), StateId = state.Id, StateName = state.Name }; model.Delimiters.Add("Comma ','", 1); model.Delimiters.Add("Colon ';'", 2); model.Delimiters.Add("Space ' '", 3); model.Delimiters.Add("Newline '\n'", 4); return(View(model)); }
public ActionResult CreateMultipleCities(AdminMarketsPlaces_CreateMultipleCitiesViewModel model) { if (ModelState.IsValid) { char delimiter = '\0'; switch (model.Delimiter) { case 1: { delimiter = ','; break; } case 2: { delimiter = ';'; break; } case 3: { delimiter = ' '; break; } case 4: { delimiter = '\n'; break; } } var cities = model.Cities.Split(delimiter).Select(s => s.Trim()).ToArray(); foreach (var city in cities) { if (db.Places.FirstOrDefault(m => m.Name == city && m.StateId == model.StateId) is null) { db.Places.Add(new Place { Name = city, StateId = model.StateId }); } } db.SaveChanges(); return(RedirectToAction("StateDetails", new { id = model.StateId })); } return(RedirectToAction("CreateMultipleCities", new { id = model.StateId })); }