public ActionResult Create([Bind(Include = "StartDate, CloseDate")] AddCycle newCycle, string Year, string Period) { try { if (ModelState.IsValid) { CycleRepository cycleRepo = new CycleRepository(); int yearPost = Int32.Parse(Year); string periodPost = Period.Trim(); switch (periodPost) { case "1": periodPost = yearPost.ToString() + " - H1"; break; case "2": periodPost = yearPost.ToString() + " - H2"; break; default: return View(newCycle); } int pf = Int32.Parse(User.Identity.Name.Substring(8)); newCycle.Year = yearPost; newCycle.Period = periodPost; if (cycleRepo.NewCycle(newCycle, pf)) { return View("Index"); } else { List<CyclePeriod> period = new List<CyclePeriod>{ new CyclePeriod {Id=1, Title="First Half : January 1 - June 30"}, new CyclePeriod {Id=2, Title="Second Half : July 1 - December 31"} }; int[] years = new int[5]; for (int i = (DateTime.Now.Year - 2), j = 0; i <= (DateTime.Now.Year + 2); i++, j++) { years[j] = i; } ViewBag.Years = years; ViewBag.Period = period; return View(newCycle); } } else { List<CyclePeriod> period = new List<CyclePeriod>{ new CyclePeriod {Id=1, Title="First Half : January 1 - June 30"}, new CyclePeriod {Id=2, Title="Second Half : July 1 - December 31"} }; int[] years = new int[5]; for (int i = (DateTime.Now.Year - 2), j = 0; i <= (DateTime.Now.Year + 2); i++, j++) { years[j] = i; } ViewBag.Years = years; ViewBag.Period = period; return View(); } } catch (Exception) { List<CyclePeriod> period = new List<CyclePeriod>{ new CyclePeriod {Id=1, Title="First Half : January 1 - June 30"}, new CyclePeriod {Id=2, Title="Second Half : July 1 - December 31"} }; int[] years = new int[5]; for (int i = (DateTime.Now.Year - 2), j = 0; i <= (DateTime.Now.Year + 2); i++, j++) { years[j] = i; } ViewBag.Years = years; ViewBag.Period = period; return View(newCycle); } }