public ActionResult Create([Bind(Include = "Id,Title,PublishDate,Content,Description,Source,SectionId,AuthorId")] StudyMaterial studyMaterial) { if (ModelState.IsValid) { db.StudyMaterials.Add(studyMaterial); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.AuthorId = new SelectList(db.Users, "Id", "FullName", studyMaterial.AuthorId); ViewBag.SectionId = new SelectList(db.Sections, "Id", "Title", studyMaterial.SectionId); return(View(studyMaterial)); }
public void SetBlogger(string id) { using (this.db) { var blogger = this.db.Users.FirstOrDefault(b => b.Id == id); if (blogger != null) { Task .Run(async() => { var isInRole = await userManager.IsInRoleAsync(blogger, "BlogAuthor"); if (!isInRole) { await userManager.AddToRoleAsync(blogger, "BlogAuthor"); db.SaveChanges(); } }) .Wait(); } } }
public void AddTrainer(TrainerModel model) { using (this.db) { var trainer = this.db.Users .FirstOrDefault(u => u.Id == model.TrainerId); var course = this.db.Courses. FirstOrDefault(c => c.Id == model.CourseId); if (trainer != null && course != null && !this.db.UsersCourses.Any(uc => uc.UserId == trainer.Id && uc.CourseId == course.Id)) { this.db.UsersCourses.Add(new UserCourse { CourseId = course.Id, UserId = trainer.Id, IsTrainer = true }); Task .Run(async() => { var isInRole = await userManager.IsInRoleAsync(trainer, "Trainer"); if (!isInRole) { await userManager.AddToRoleAsync(trainer, "Trainer"); db.SaveChanges(); } }) .Wait(); this.db.SaveChanges(); } } }