public ActionResult Edit(GridEditView view, bool error = false)
        {
            try
            {
                ViewBag.HideScreen = false;

                if (Equals(view.IdGrid, Guid.Empty))
                {
                    if (Equals(view.IdSemester, Guid.Empty))
                    {
                        throw new Exception("Parâmetro 'IdSemester' inválido");
                    }

                    if (Equals(view.IdCourse, Guid.Empty))
                    {
                        throw new Exception("Parâmetro 'IdCourse' inválido");
                    }
                }

                var semester = Semester.Find(view.IdSemester);

                if (semester == null)
                {
                    throw new Exception("Semestre não encontrado");
                }

                Class filter = new Class()
                {
                    Course = new Course()
                    {
                        IdCourse = view.IdCourse
                    }
                };

                var classList = Class.List(filter, by => by.Name);

                if (classList == null)
                {
                    throw new Exception("As disciplinas não foram listadas");
                }

                var teachers = Teacher.List(sort: by => by.Name);

                if (teachers == null)
                {
                    throw new Exception("Os professores não foram listados");
                }

                view.TeacherSelectList = new SelectList(teachers, "IdTeacher", "Name");
                view.ClassSelectList   = new SelectList(classList, "IdClass", "Name");
                view.TimeSelectList    = new SelectList(Grid.TimeList(semester.Period.Trim()));
                view.WeekdaySelectList = new SelectList(Grid.WeekdayList(), "Weekday", "WeekdayName");
                view.StatusSelectList  = new SelectList(Grid.StatusList(), Grid.REGISTERED);
                view.StatusSelected    = Grid.REGISTERED;

                if (error)
                {
                    ViewBag.Message = "Não foi possível salvar o registro. Analise os erros.";
                    return(View("Edit", view));
                }

                ModelState.Clear();

                if (Equals(view.IdGrid, Guid.Empty))
                {
                    return(View("Edit", view));
                }

                var item = Grid.Find(view.IdGrid);

                if (item == null)
                {
                    throw new Exception("Grade não encontrada");
                }

                if (item.Semester.IsActual == false)
                {
                    throw new Exception("Semestre não é atual");
                }

                view.TeacherSelectList = new SelectList(teachers, "IdTeacher", "Name", item.Teacher.IdTeacher);
                view.TeacherSelected   = item.Teacher.IdTeacher;
                view.ClassSelectList   = new SelectList(classList, "IdClass", "Name", item.Class.IdClass);
                view.ClassSelected     = item.Class.IdClass;
                view.TimeSelectList    = new SelectList(Grid.TimeList(semester.Period), item.Time.Trim());
                view.TimeSelected      = item.Time.Trim();
                view.WeekdaySelectList = new SelectList(Grid.WeekdayList(), "Weekday", "WeekdayName", item.Weekday);
                view.WeekdaySelected   = item.Weekday;
                view.StatusSelectList  = new SelectList(Grid.StatusList(), item.Status.Trim());
                view.StatusSelected    = item.Status.Trim();
                view.Price             = item.Price;
            }
            catch (Exception e)
            {
                object[] parameters = { view, error };
                string   notes      = LogHelper.Notes(parameters, e.Message);
                Log.Add(Log.TYPE_ERROR, "SistemaMatricula.Controllers.GridController.Edit", notes);
                ViewBag.Message    = "Não foi possível realizar a solicitação. Erro de execução.";
                ViewBag.HideScreen = true;
            }

            return(View("Edit", view));
        }
        public ActionResult Update(GridEditView view)
        {
            try
            {
                if (ModelState.IsValid == false)
                {
                    return(Edit(view, true));
                }

                var semester = Semester.Find(view.IdSemester);

                if (semester.IsActual == false)
                {
                    throw new Exception("Semestre não é atual");
                }

                view.Class = new Class()
                {
                    IdClass = view.ClassSelected
                };
                view.Weekday = view.WeekdaySelected;
                view.Time    = view.TimeSelected.Trim();
                view.Teacher = new Teacher()
                {
                    IdTeacher = view.TeacherSelected
                };
                view.Semester = new Semester()
                {
                    IdSemester = view.IdSemester
                };
                view.Status = view.StatusSelected.Trim();

                if (Equals(view.IdGrid, Guid.Empty))
                {
                    var insert = Grid.Add(view);

                    if (insert == false)
                    {
                        return(Edit(view, true));
                    }

                    return(RedirectToAction("Index", "Grid",
                                            new GridIndexView {
                        CourseSelected = view.IdCourse, SemesterSelected = view.Semester.IdSemester
                    }));
                }

                var update = Grid.Update(view);

                if (update == false)
                {
                    return(Edit(view, true));
                }

                return(RedirectToAction("Index", "Grid",
                                        new GridIndexView {
                    CourseSelected = view.IdCourse, SemesterSelected = view.Semester.IdSemester
                }));
            }
            catch (Exception e)
            {
                string notes = LogHelper.Notes(view, e.Message);
                Log.Add(Log.TYPE_ERROR, "SistemaMatricula.Controllers.GridController.Update", notes);
                ViewBag.Message    = "Não foi possível realizar a solicitação. Erro de execução.";
                ViewBag.HideScreen = true;
            }

            return(View("Edit"));
        }