Ejemplo n.º 1
0
        public ActionResult Create(CreateCourseModel model)
        {
            var userID = User.Identity.GetUserId();
            if (userID == null)
            {
                ModelState.AddModelError("", "The user is not signed in.");
            }
            AppCourse appCourse = new AppCourse();
            if (ModelState.IsValid)
            {
                if (UserManager.IsInRole(userID, "Administrators"))
                {
                    appCourse.AppUserID = model.AppUserID;
                } else
                {
                    appCourse.AppUserID = userID;
                }
                appCourse.AppCategoryID = model.AppCategoryID;
                appCourse.AppCourseStatusID = db.AppCourseStatuss.Where(x => x.Status == "New").FirstOrDefault().ID;
                appCourse.Course = model.Course;
                appCourse.Headline = model.Headline;
                appCourse.Description = model.Description;
                appCourse.CourseImage = model.CourseImage;
                appCourse.CreatedAt = DateTime.Now;
                appCourse.Location = model.Location;
                appCourse.Price = model.Price;
                appCourse.Rating = 0;
                appCourse.MaxAttendance = model.MaxAttendance;

                //Adding tags to course
                if (appCourse.AppTags == null)
                {
                    appCourse.AppTags = new List<AppTag>();
                }
                foreach (int id in model.TagIDs)
                {
                    AppTag tag = db.AppTags.Where(x => x.ID == id).FirstOrDefault();
                    appCourse.AppTags.Add(tag);
                }

                //saving course in database
                AppCourse newCourse = db.Courses.Add(appCourse);
                db.SaveChanges();

                if (User.IsInRole("Administrators"))
                {
                    return RedirectToAction("Index");
                } else if (User.IsInRole("Teacher"))
                {
                    return RedirectToAction("Index", "Course", new { area = "Teachers" });
                }

            }

            ViewBag.TagID = new MultiSelectList(db.AppTags, "ID", "Tag");
            ViewBag.AppCategoryID = new SelectList(db.AppCategorys, "ID", "Category", appCourse.AppCategoryID);
            ViewBag.AppCourseStatusID = new SelectList(db.AppCourseStatuss, "ID", "Status", appCourse.AppCourseStatusID);
            ViewBag.AppUserID = new SelectList(db.Users, "Id", "FullName", appCourse.AppUserID);
            return View(model);
        }
Ejemplo n.º 2
0
        public ActionResult Create( CreateCourseModel model, string password)
        {
            AppCourse appCourse = new AppCourse();
            if (ModelState.IsValid)
            {
                appCourse.AppUserID = model.AppUserID;
                appCourse.AppCategoryID = model.AppCategoryID;
                appCourse.AppCourseStatusID = db.AppCourseStatuss.Where(x => x.Status == "New").FirstOrDefault().ID;
                appCourse.Course = model.Course;
                appCourse.Headline = model.Headline;
                appCourse.Description = model.Description;
                appCourse.CourseImage = model.CourseImage;
                appCourse.CreatedAt = DateTime.Now;
                appCourse.Location = model.Location;
                appCourse.Price = model.Price;
                appCourse.Rating = 0;
                appCourse.MaxAttendance = model.MaxAttendance;

                //Adding tags to course
                if(appCourse.AppTags == null){
                    appCourse.AppTags = new List<AppTag>();
                }
                foreach (int id in model.TagIDs)
                {
                    AppTag tag = db.AppTags.Where(x => x.ID == id).FirstOrDefault();
                    appCourse.AppTags.Add(tag);
                }

                //saving course in database
                db.Courses.Add(appCourse);
                db.SaveChanges();

                return RedirectToAction("Index");
            }

            ViewBag.AppCategoryID = new SelectList(db.AppCategorys, "ID", "Category", appCourse.AppCategoryID);
            ViewBag.AppCourseStatusID = new SelectList(db.AppCourseStatuss, "ID", "Status", appCourse.AppCourseStatusID);
            ViewBag.AppUserID = new SelectList(db.Users, "Id", "FirstName", appCourse.AppUserID);
            return View(model);
        }