//public Block GetBlockLoaded(int id) //{ // var block = context.Blocks.Include("Teacher").Include("Grade").Where(b => b.BlockId == id).FirstOrDefault(); // return block; //} public void SaveBlock(Block block) { if (block.BlockId == 0) { context.Blocks.Add(block); } foreach (BlockDay day in block.ScheduledDays) { if (day.BlockDayId==0) { context.BlockDays.Add(day); } } context.SaveChanges(); }
public ActionResult Create(FormCollection collection) { try { // TODO: Add insert logic here var block = new Block { Name = collection.Get("Name"), Description = collection.Get("Description"), IsActive = string.Compare(collection.Get("IsActive"), "false") == 0 ? false : true }; DateTime start, end; if (!DateTime.TryParse(collection.Get("StartDate"), out start) || !DateTime.TryParse(collection.Get("EndDate"), out end)) { ModelState.AddModelError("", "Start date and End date must be dates with the format [dd/mm/yyyy]."); FillDropDowns(); return View(block); } block.StartDate = start; block.EndDate = end; if (start > end) { ModelState.AddModelError("", "The end date must be later than the start date."); FillDropDowns(); return View(block); } int teacherId, gradeId; if (!int.TryParse(collection.Get("teacherId"), out teacherId) || !int.TryParse(collection.Get("gradeId"), out gradeId)) { ModelState.AddModelError("", "The selected choices for Teacher and/or Grade are not correct."); FillDropDowns(); return View(block); } if (gradeId == 0 || teacherId == 0) { ModelState.AddModelError("", "The selected choices for Teacher and/or Grade are not correct."); FillDropDowns(); return View(block); } if (Conversor.ConvertToBool(collection.Get("chkSunday"))) { var day = new BlockDay(); if (!Conversor.ConvertToDateTime(collection.Get("startSunday"), day.StartTime) || !Conversor.ConvertToDateTime(collection.Get("startSunday"), day.EndTime)) { ModelState.AddModelError("", "The times for Sunday are incorrect."); FillDropDowns(); return View(block); } } //var teacher = repository.GetTeacher(teacherId); //var grade = repository.GetGradeLevel(gradeId); //if (teacher == null || grade == null) //{ // ModelState.AddModelError("", "The selected choices for Teacher and/or Grade are not correct."); // FillDropDowns(); // return View(block); //} //block.Teacher = teacher; //block.Grade = grade; block.TeacherId = teacherId; block.GradeLevelId = gradeId; bRepository.SaveBlock(block); return RedirectToAction("Index"); } catch { FillDropDowns(); return View(); } }