public async Task <ActionResult> Create(AddEditCourseModel model)
        {
            Provider provider = db.Providers.Find(userContext.ItemId);

            if (provider == null)
            {
                return(HttpNotFound());
            }

            CheckModel(model);

            if (ModelState.IsValid)
            {
                if (!String.IsNullOrEmpty(model.LearningAimId))
                {
                    LearningAim learningAim = db.LearningAims.Find(model.LearningAimId);
                    if (learningAim == null)
                    {
                        return(HttpNotFound());
                    }
                }
                Course course = model.ToEntity(db);
                course.ProviderId           = provider.ProviderId;
                course.RecordStatusId       = (Int32)Constants.RecordStatus.Pending;
                course.AddedByApplicationId = (Int32)Constants.Application.Portal;

                if (String.IsNullOrEmpty(model.LearningAimId))
                {
                    List <String> LearnDirectClassificationCodes = new List <String>();
                    if (!String.IsNullOrEmpty(model.LearnDirectClassificationId1))
                    {
                        LearnDirectClassificationCodes.Add(model.LearnDirectClassificationId1);
                    }
                    if (!String.IsNullOrEmpty(model.LearnDirectClassificationId2))
                    {
                        LearnDirectClassificationCodes.Add(model.LearnDirectClassificationId2);
                    }
                    if (!String.IsNullOrEmpty(model.LearnDirectClassificationId3))
                    {
                        LearnDirectClassificationCodes.Add(model.LearnDirectClassificationId3);
                    }
                    if (!String.IsNullOrEmpty(model.LearnDirectClassificationId4))
                    {
                        LearnDirectClassificationCodes.Add(model.LearnDirectClassificationId4);
                    }
                    if (!String.IsNullOrEmpty(model.LearnDirectClassificationId5))
                    {
                        LearnDirectClassificationCodes.Add(model.LearnDirectClassificationId5);
                    }
                    Int32 i = 1;
                    foreach (LearnDirectClassification classification in LearnDirectClassificationCodes.Select(ld => db.LearnDirectClassifications.Find(ld)))
                    {
                        CourseLearnDirectClassification cld = new CourseLearnDirectClassification
                        {
                            LearnDirectClassification = classification,
                            ClassificationOrder       = i
                        };
                        course.CourseLearnDirectClassifications.Add(cld);
                        i++;
                    }
                }

                db.Courses.Add(course);
                await db.SaveChangesAsync();

                List <String> messages = model.GetWarningMessages();
                if (messages.Count == 0)
                {
                    ShowGenericSavedMessage();
                }
                else
                {
                    // Add a blank entry at the beginning so the String.Join starts with <br /><br />
                    messages.Insert(0, "");
                    SessionMessage.SetMessage(AppGlobal.Language.GetText(this, "SaveSuccessfulWithWarnings", "Your changes were saved successfully with the following warnings:") + String.Join("<br /><br />", messages), SessionMessageType.Success);
                }

                // If user clicked "Create" then take them to Course List
                if (Request.Form["Create"] != null)
                {
                    return(RedirectToAction("List"));
                }

                // User clicked "Create and Add Opportunity" - Take them to Create Opportunity page
                return(RedirectToAction("Create", "Opportunity", new { id = course.CourseId }));
            }

            // Populate drop downs
            GetLookups(model);

            return(View(model));
        }
        public async Task <ActionResult> Edit(Int32 id, AddEditCourseModel model)
        {
            Provider provider = db.Providers.Find(userContext.ItemId);

            if (provider == null)
            {
                return(HttpNotFound());
            }

            if (model.CourseId != id)
            {
                return(HttpNotFound());
            }

            Course course = db.Courses.Find(id);

            if (course == null)
            {
                return(HttpNotFound());
            }

            CheckModel(model);

            if (ModelState.IsValid)
            {
                course = model.ToEntity(db);
                if (course.ProviderId != userContext.ItemId)
                {
                    // User is trying to change the VenueId (or the context has changed)
                    return(HttpNotFound());
                }

                // Delete Existing CourseLearnDirectClassifications
                // Normally I would work out which already exist and just write the changes but because of the ordering it's easier just to delete them all and re-write them all
                foreach (CourseLearnDirectClassification ld in course.CourseLearnDirectClassifications.ToList())
                {
                    course.CourseLearnDirectClassifications.Remove(ld);
                }

                // Add any new Learn Direct classifications
                if (String.IsNullOrEmpty(model.LearningAimId))
                {
                    List <String> LearnDirectClassificationCodes = new List <String>();
                    if (!String.IsNullOrEmpty(model.LearnDirectClassificationId1))
                    {
                        LearnDirectClassificationCodes.Add(model.LearnDirectClassificationId1);
                    }
                    if (!String.IsNullOrEmpty(model.LearnDirectClassificationId2))
                    {
                        LearnDirectClassificationCodes.Add(model.LearnDirectClassificationId2);
                    }
                    if (!String.IsNullOrEmpty(model.LearnDirectClassificationId3))
                    {
                        LearnDirectClassificationCodes.Add(model.LearnDirectClassificationId3);
                    }
                    if (!String.IsNullOrEmpty(model.LearnDirectClassificationId4))
                    {
                        LearnDirectClassificationCodes.Add(model.LearnDirectClassificationId4);
                    }
                    if (!String.IsNullOrEmpty(model.LearnDirectClassificationId5))
                    {
                        LearnDirectClassificationCodes.Add(model.LearnDirectClassificationId5);
                    }
                    Int32 i = 1;
                    foreach (LearnDirectClassification classification in LearnDirectClassificationCodes.Select(ld => db.LearnDirectClassifications.Find(ld)))
                    {
                        CourseLearnDirectClassification cld = new CourseLearnDirectClassification
                        {
                            LearnDirectClassification = classification,
                            ClassificationOrder       = i
                        };
                        course.CourseLearnDirectClassifications.Add(cld);
                        i++;
                    }
                }

                course.ModifiedByUserId     = Permission.GetCurrentUserId();
                course.ModifiedDateTimeUtc  = DateTime.UtcNow;
                course.AddedByApplicationId = (Int32)Constants.Application.Portal;

                db.Entry(course).State = EntityState.Modified;
                await db.SaveChangesAsync();

                List <String> messages = model.GetWarningMessages();
                if (messages.Count == 0)
                {
                    ShowGenericSavedMessage();
                }
                else
                {
                    // Add a blank entry at the beginning so the String.Join starts with <br /><br />
                    messages.Insert(0, "");
                    SessionMessage.SetMessage(AppGlobal.Language.GetText(this, "SaveSuccessfulWithWarnings", "Your changes were saved successfully with the following warnings:") + String.Join("<br /><br />", messages), SessionMessageType.Success);
                }

                return(RedirectToAction("List"));
            }

            // Populate drop downs
            GetLookups(model);

            return(View(model));
        }