public ActionResult AddState2(StateVM sVM) { IEnumerable <State> states = StateRepository.GetAll(); IEnumerable <State> allStates = StateRepository.AllStates(); if (states.Any(s => s.StateAbbreviation == sVM.State.StateAbbreviation)) { ModelState.AddModelError("StateAbbreviation", "This State Abbreviation is already in use."); } if (!allStates.Any(s => s.StateAbbreviation == sVM.State.StateAbbreviation)) { ModelState.AddModelError("StateAbbreviation", "Please choose a valid state to add."); } foreach (var item in allStates) { if (item.StateAbbreviation == sVM.State.StateAbbreviation) { sVM.State = item; } } if (ModelState.IsValid) { StateRepository.Add(sVM.State); return(RedirectToAction("States")); } return(View("AddState2", sVM)); }
public ActionResult AddState2() { StateVM svm = new StateVM(); svm.SetStateItems(StateRepository.GetAll()); svm.SetAllStateItems(StateRepository.AllStates()); svm.SetAvailableStateItems(StateRepository.AvailableStates()); return(View(svm)); }