public ActionResult Create(Course course)
 {
     try
     {
         db.Courses.Add(course);
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     catch
     {
         return View(course);
     }
     return View();
 }
示例#2
0
        public ActionResult Create( CourseCreateEdit newCourse )
        {
            if( newCourse.Name == null || newCourse.Number_of_Hours < 0 || newCourse.Number_of_Classes < 0 || newCourse.Description == null ) {
                return View( newCourse );
            }
            List<Person> studentsList = new List<Person>();
            List<Person> teachersList = new List<Person>();
            List<Program> programList = new List<Program>();
            List<Blog> blogsList = new List<Blog>();
            for( int i = 0; i < newCourse.Chosen_Teachers.Count(); i++ ) {
                if( newCourse.Chosen_Teachers[i] ) {
                    string id = newCourse.Teachers[i].Id;
                    Person teacher = db.Users.Where( p => p.Id == id ).FirstOrDefault();
                    teachersList.Add( teacher );
                }
            }
            for( int i = 0; i < newCourse.Chosen_Programs.Count(); i++ ) {
                if( newCourse.Chosen_Programs[i] ) {
                    int id = newCourse.Programs[i].ID;
                    Program program = db.Program.Where( p => p.ID == id ).FirstOrDefault();
                    programList.Add( program );
                }
            }

            for( int i = 0; i < newCourse.Chosen_Blogs.Count(); i++ ) {
                if( newCourse.Chosen_Blogs[i] ) {
                    int id = newCourse.Blogs[i].ID;
                    Blog blog = db.Blog.Where( p => p.ID == id ).FirstOrDefault();
                    blogsList.Add( blog );
                }
            }

            var course = new Course {
                Name = newCourse.Name,
                Description = newCourse.Description,
                BasePart = newCourse.Base_Part,
                Date_and_Time = newCourse.Date_and_Time,
                Number_of_Classes = newCourse.Number_of_Classes,
                Number_of_Hours = newCourse.Number_of_Hours,
                Report_Type = newCourse.Report_Type,
                Teachers = teachersList,
                Programs = programList,
                Blogs = blogsList
            };
            if( newCourse.Chosen_Programs.ToArray().Contains( true ) )
                course.Grade = newCourse.Chosen_Programs.ToList().IndexOf( true ) + 3;

            db.Course.Add( course );
            db.SaveChanges();
            return RedirectToAction( "Index", "Courses" );
        }
示例#3
0
        public ActionResult Index( int? grade, string basePart )
        {
            ViewBag.Title = "Courses";
            ViewBag.Grade = grade;
            ViewBag.BasePart = basePart;
            Course[] courses = new Course[] { };
            if( grade != null && basePart != null ) {
                var prog = db.Program.Where( p => p.Name == grade.ToString() + " курс" ).FirstOrDefault();
                if( prog != null )
                    courses = prog.Courses.Where( p => p.BasePart == basePart || p.BasePart == null ).ToArray();
                else
                    courses = db.Course.ToArray();
            } else {
                if( grade != null ) {
                    var prog = db.Program.Where( p => p.Name == grade.ToString() + " курс" ).FirstOrDefault();
                    prog.Courses.ToArray();
                    if( prog != null )
                        courses = prog.Courses.ToArray();
                    else
                        courses = db.Course.ToArray();
                } else {
                    if( basePart != null ) {
                        courses = db.Course.Where( p => p.BasePart == basePart || p.BasePart == null ).ToArray();
                    } else {
                        courses = db.Course.ToArray();
                    }
                }
            }

            return View( courses );
        }