public ActionResult Create([Bind(Include = "routeStopId,busRouteCode,busStopNumber,offsetMinutes")] routeStop routeStop) { if (ModelState.IsValid) { //Examines cookie or session variables to assign to routeStop.busRouteCode if (Request.Cookies["busRoutecode"] != null) { routeStop.busRouteCode = Request.Cookies["busRouteCode"].Value; } else if (Session["busRouteCode"] != null) { routeStop.busRouteCode = Session["busRouteCode"].ToString(); } else { TempData["message"] = "Please select Bus Route.."; return(RedirectToAction("index", "SPBusRoute")); } db.routeStops.Add(routeStop); db.SaveChanges(); return(RedirectToAction("Index")); } //Commented because this auto generated code doesn't need any more. //ViewBag.busRouteCode = new SelectList(db.busRoutes, "busRouteCode", "routeName", routeStop.busRouteCode); ViewBag.busStopNumber = new SelectList(db.busStops, "busStopNumber", "location", routeStop.busStopNumber); return(View(routeStop)); }
public ActionResult Create([Bind(Include = "busRouteCode,routeName")] busRoute busRoute) { if (ModelState.IsValid) { db.busRoutes.Add(busRoute); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(busRoute)); }
public ActionResult Create([Bind(Include = "routeScheduleId,busRouteCode,startTime,isWeekDay,comments")] routeSchedule routeSchedule) { if (ModelState.IsValid) { db.routeSchedules.Add(routeSchedule); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.busRouteCode = new SelectList(db.busRoutes, "busRouteCode", "routeName", routeSchedule.busRouteCode); return(View(routeSchedule)); }
public ActionResult Create([Bind(Include = "busStopNumber,location,locationHash,goingDowntown")] busStop busStop) { if (ModelState.IsValid) { // Calculating byte sum of a String and it's going to be a locationHash int byteSum = 0; for (int i = 0; i < busStop.location.Length; i++) { byteSum += (byte)busStop.location[i]; } busStop.locationHash = byteSum; db.busStops.Add(busStop); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(busStop)); }
public ActionResult Create(trip trip) { //Does exactly same thing that has done in first Create ActionResult for dropdown lists. string tempBusRouteCode = Session["busRouteCode"].ToString(); var routeSchedule = from r in db.routeSchedules where r.busRouteCode == tempBusRouteCode orderby r.isWeekDay descending, r.startTime select r; List <StartTime> startTimeList = new List <StartTime>(); string timeString = ""; foreach (var time in routeSchedule) { if (time.startTime == null) { continue; } timeString = time.startTime.ToString("hh"); timeString += "-"; timeString += time.startTime.ToString("mm"); if (time.isWeekDay) { timeString += " weekday"; } else { timeString += " weekend"; } startTimeList.Add(new StartTime() { routeScheduleId = time.routeScheduleId.ToString(), timeString = timeString }); } var tripTimeListOnly = new trip { timeList = new SelectList(startTimeList, "routeScheduleId", "timeString") }; var drivers = from d in db.drivers orderby d.fullName select d; List <SelectListItem> listDrivers = new List <SelectListItem>(); int driverIndex = 0; foreach (var driver in drivers) { listDrivers.Insert(driverIndex, new SelectListItem { Value = driver.driverId.ToString(), Text = driver.fullName }); driverIndex++; } ViewBag.drivers = listDrivers; var buses = from b in db.buses where b.status == "available" orderby b.busNumber select b; ViewBag.buses = buses; //Check the ModelState and generate error message when it is invalid if (ModelState.IsValid) { try { db.trips.Add(trip); db.SaveChanges(); //When succeeds, put a success message and goes to the refreshed list TempData["message"] = "New trip added!"; return(RedirectToAction("Index")); } catch (Exception ex) { //At any exception in that "try" clause, this takes innerException into ModelState, //generate exception message to the TempData and returns current view model to Create View ModelState.AddModelError("", ex.InnerException.Message); TempData["message"] = "exception getting Trips: " + ex.GetBaseException().Message; return(View(tripTimeListOnly)); } } //When the ModelState is invalid, put a message to the TempData and returns current view model to Create View TempData["message"] = "Model State is invalid"; return(View(tripTimeListOnly)); }