public ActionResult EditModule(EditModuleViewModel model, int? id) { var module = db.Modules.FirstOrDefault(c => c.Id == id); ViewBag.AtEarliest = (module != null ? (DateTime.Today.AddDays(1) > module.StartDate ? DateTime.Today.AddDays(1) : module.StartDate) : DateTime.Today.AddDays(1)); ViewBag.AtLatest = (module != null ? (DateTime.Today.AddDays(2) < module.EndDate ? module.EndDate : DateTime.Today.AddDays(2)) : DateTime.Today.AddDays(2)); Menu(Home: true); SetBreadcrumbs( one: new MenyItem { Link = "~/Teacher/", Text = "Se alla kurser" }, three: new MenyItem { Link = "~/Teacher/Module/" + id, Text = "Tillbaka till modul" }); if (id == null) { ViewBag.Error = "Inget id har angivits"; return View("~/Views/Error/Index.cshtml"); } if (!ModelState.IsValid) { ViewBag.Id = (int)id; return View(model); } model.StartDate = new DateTime(model.StartDate.Year, model.StartDate.Month, model.StartDate.Day, 0, 0, 0); model.EndDate = new DateTime(model.EndDate.Year, model.EndDate.Month, model.EndDate.Day, 23, 59, 0); bool hasError = false; if (module == null) { ModelState.AddModelError("", "Ingen module funnen"); hasError = true; } if (model.StartDate < DateTime.Today.AddDays(1)) { ModelState.AddModelError("StartDate", "Startdatum kan tyvärr ej starta innan morgondagen, pga. planeringstid"); hasError = true; } if (model.EndDate < model.StartDate) { ModelState.AddModelError("EndDate", "Slutdatumet kan ej vara innan startdatumet"); hasError = true; } Course course = db.Courses.FirstOrDefault(c => c.Id == module.CourseId); if (course == null) { ModelState.AddModelError("", "Ingen tillhörande kurs funnen"); hasError = true; } if (model.StartDate < course.StartDate) { ModelState.AddModelError("StartDate", "Startdatum kan tyvärr ej starta innan kursen"); hasError = true; } if (model.EndDate > course.EndDate) { ModelState.AddModelError("EndDate", "Slutdatumet kan ej överstrida kursens slutdatum"); hasError = true; } if (hasError) { ViewBag.Id = (int)id; return View(model); } module.Description = model.Description; module.EndDate = model.EndDate; module.StartDate = model.StartDate; module.Name = model.Name; db.Entry(module).State = EntityState.Modified; db.SaveChanges(); return Redirect("~/Teacher/Module/"+ module.Id); }
public ActionResult EditModule(int id) { var model = new EditModuleViewModel(id); return(View(model)); }
public ActionResult EditModule(int? id) { if (id == null) { ViewBag.Error = "Inget id har angivits"; return View("~/Views/Error/Index.cshtml"); } var module = db.Modules.FirstOrDefault(c => c.Id == id); if (module == null) { ViewBag.Error = "Ingen module har hittats"; return View("~/Views/Error/Index.cshtml"); } Course course = module.Course; if (course == null) { ViewBag.Error = "Ingen kurs förälder har hittats"; return View("~/Views/Error/Index.cshtml"); } EditModuleViewModel model = new EditModuleViewModel(); model.Description = module.Description; model.Name = module.Name; model.StartDate = module.StartDate; model.EndDate = module.EndDate; ViewBag.Id = (int)id; Menu(Home: true); SetBreadcrumbs( one: new MenyItem { Link = "~/Teacher/", Text = "Se alla kurser" }, two: new MenyItem { Link = "~/Teacher/Course/" + course.Id, Text = course.Name }, three: new MenyItem { Link = "~/Teacher/Module/" + module.Id, Text = module.Name }); ViewBag.AtEarliest = (DateTime.Today.AddDays(1) > course.StartDate ? DateTime.Today.AddDays(1) : course.StartDate); ViewBag.AtLatest = (DateTime.Today.AddDays(2) < course.EndDate ? course.EndDate : DateTime.Today.AddDays(2)); return View(model); }