// GET: /Event/Create public ActionResult Create() { ViewBag.Country = new SelectList(UoW.Countries.GetAll().ToList(), "CountryId", "Name"); ViewBag.Province = new SelectList(UoW.ProvinceStates.GetAll().OrderBy(p => p.ProvinceStateId).Take(13), "ProvinceStateId", "Name"); ViewBag.State = new SelectList(UoW.ProvinceStates.GetAll().OrderBy(p => p.ProvinceStateId).Skip(13), "ProvinceStateId", "Name"); var model = new CreateEventViewModel(); return View(model); }
public ActionResult Create(CreateEventViewModel model, FormCollection form) { ViewBag.Country = new SelectList(UoW.Countries.GetAll().ToList(), "CountryId", "Name"); ViewBag.Province = new SelectList(UoW.ProvinceStates.GetAll().OrderBy(p => p.ProvinceStateId).Take(13), "ProvinceStateId", "Name"); ViewBag.State = new SelectList(UoW.ProvinceStates.GetAll().OrderBy(p => p.ProvinceStateId).Skip(13), "ProvinceStateId", "Name"); // grab the form's dropdown lists selected value. var Country = Int32.Parse(form["Country"]); var Province = Int32.Parse(form["Province"]); var State = Int32.Parse(form["State"]); // check which country is selected. If shipping and home addresses are the same Country, // some of the form collection attributes will bu null/zero. int CountryId = Country; int ProvinceOrState = CountryId == 1 ? Province : State; var Address = new Address() { Street1 = model.Address.Street1, Street2 = model.Address.Street2, City = model.Address.City, PostalZipCode = model.Address.PostalZipCode, ProvinceStateId = ProvinceOrState, CountryId = Country }; var Event = new Models.Event() { Address = Address, Date = model.Date, Name = model.Name, EventDetails = model.EventDetails }; if (ModelState.IsValid) { try { db.Events.Add(Event); db.SaveChanges(); TempData["toast"] = "<script> $(document).ready(function () {" + "toastr.options = { 'positionClass': 'toast-bottom-right' };" + "toastr.success('Event was created successfully!');});</script>"; return RedirectToAction("EmployeeEventsIndex", "Events"); } catch (Exception) { TempData["toast"] = "<script> $(document).ready(function () {" + "toastr.options = { 'positionClass': 'toast-bottom-right' };" + "toastr.error('Error saving the event. Please try again.');});</script>"; return View("Create", model); } } return View("Create", model); }