public ActionResult Create(CreateLocationViewModel model) { Location location = new Location(); string country = Location.ReverseGeocodeCountry(model.Latitude, model.Longitude); if (!country.Equals("US") || country.Equals(null) || country.Equals("")) { ModelState.AddModelError("Longitude", "The location must be within the United States of America."); } if (ModelState.IsValid) { // transfer info location.Label = model.Label; location.Latitude = model.Latitude; location.Longitude = model.Longitude; location.Description = model.Description; location.Difficulty = (Location.DifficultyRatings) model.Difficulty; // initialize DateTime Stamps location.DateCreated = DateTime.UtcNow; location.DateModified = location.DateCreated; location.Rating(); location.UpVotes(); location.DownVotes(); string abbrev = Location.ReverseGeocodeState(location); location.State = db.States.Where(s => s.StateID.Equals(abbrev)).SingleOrDefault(); // save changes db.Locations.Add(location); db.SaveChanges();//must save before we move on so that location gets an ID // edit Recreations System.Diagnostics.Debug.WriteLine("SelectedRecreations = " + GetArrayFormattedString(model.SelectedFeatures)); EditRecreationsFor(location, model.SelectedRecreations); // edit NaturalFeatures System.Diagnostics.Debug.WriteLine("SelectedFeatures = " + GetArrayFormattedString(model.SelectedFeatures)); EditNaturalFeaturesFor(location, model.SelectedFeatures ?? new List<string>()); return RedirectToAction("Index"); } ModelState.AddModelError("Overall", "You are missing one or more required fields."); CreateLocationViewModel viewModel = new CreateLocationViewModel(); viewModel.Label = model.Label; viewModel.Latitude = model.Latitude; viewModel.Longitude = model.Longitude; viewModel.Description = model.Description; viewModel.Difficulty = model.Difficulty; viewModel.SelectedRecreations = model.SelectedRecreations ?? new List<String>(); viewModel.AllRecreations = db.Recreations .Select(r => r.Label) .ToList(); viewModel.SelectedFeatures = model.SelectedFeatures ?? new List<String>(); viewModel.AllNaturalFeatures = db.NaturalFeatures .Select(nf => nf.Name) .ToList(); return View(viewModel); }
public ActionResult Create() { CreateLocationViewModel viewModel = new CreateLocationViewModel(); viewModel.SelectedRecreations = new List<String>(); viewModel.AllRecreations = db.Recreations .Select(r => r.Label) .ToList(); viewModel.SelectedFeatures = new List<string>(); viewModel.AllNaturalFeatures = db.NaturalFeatures .Select(nf => nf.Name) .ToList(); return View(viewModel); }