public async Task <IActionResult> Create(CreateCoursesVM createCoursesVM, List <int> List) { ViewBag.Categories = _db.Categories.ToList(); if (!ModelState.IsValid) { return(View()); } if (ModelState["Photo"].ValidationState == Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Invalid) { return(View()); } if (!createCoursesVM.Photo.IsImage()) { ModelState.AddModelError("Photo", "Zehmet olmasa shekil formati sechin"); return(View()); } if (createCoursesVM.Photo.MaxLength(200)) { ModelState.AddModelError("Photo", "Shekilin olchusu max 200kb ola biler"); return(View()); } string path = Path.Combine("img", "course"); string filelName = await createCoursesVM.Photo.SaveImg(_env.WebRootPath, path); Courses newCourses = new Courses { Image = filelName, Title = createCoursesVM.Title, Description = createCoursesVM.Description, AboutCourses = createCoursesVM.AboutCourses, HowToApply = createCoursesVM.HowToApply, Certification = createCoursesVM.Certification, Starts = createCoursesVM.Starts, Duration = createCoursesVM.Duration, ClassDuration = createCoursesVM.ClassDuration, SkillLevel = createCoursesVM.SkillLevel, Language = createCoursesVM.Language, Students = createCoursesVM.Students, Assesments = createCoursesVM.Assesments, CourseFee = createCoursesVM.CourseFee }; List <CategoryCourses> categoryCourses = new List <CategoryCourses>(); foreach (var item in List) { CategoryCourses newCategoryCourse = new CategoryCourses { CoursesId = newCourses.Id, CategoryId = item }; categoryCourses.Add(newCategoryCourse); } newCourses.CategoryCourses = categoryCourses; await _db.Courses.AddAsync(newCourses); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Update(int?id, Courses courses, List <int> List) { ViewBag.Categories = _db.Categories.ToList(); if (id == null) { return(NotFound()); } Courses dbCourses = _db.Courses.Include(d => d.CategoryCourses).ThenInclude(x => x.Courses).FirstOrDefault(c => c.Id == id); if (dbCourses == null) { return(NotFound()); } if (courses.Photo != null) { if (ModelState["Photo"].ValidationState == Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Invalid) { return(View()); } if (!courses.Photo.IsImage()) { ModelState.AddModelError("Photo", "Zehmet olmasa shekil formati sechin"); return(View()); } if (courses.Photo.MaxLength(2000)) { ModelState.AddModelError("Photo", "Shekilin olchusu max 200kb ola biler"); return(View()); } string path = Path.Combine("img", "course"); Helper.DeleteImage(_env.WebRootPath, path, dbCourses.Image); string fileName = await courses.Photo.SaveImg(_env.WebRootPath, path); dbCourses.Image = fileName; } dbCourses.Title = courses.Title; dbCourses.Description = courses.Description; dbCourses.AboutCourses = courses.Description; dbCourses.HowToApply = courses.HowToApply; dbCourses.Certification = courses.Certification; dbCourses.Starts = courses.Starts; dbCourses.Duration = courses.Duration; dbCourses.ClassDuration = courses.ClassDuration; dbCourses.SkillLevel = courses.SkillLevel; dbCourses.Language = courses.Language; dbCourses.Students = courses.Students; dbCourses.Assesments = courses.Assesments; dbCourses.CourseFee = courses.CourseFee; List <CategoryCourses> categoryCourses = new List <CategoryCourses>(); foreach (var item in List) { CategoryCourses newCategoryCourses = new CategoryCourses { CoursesId = dbCourses.Id, CategoryId = item }; categoryCourses.Add(newCategoryCourses); } dbCourses.CategoryCourses = categoryCourses; await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }