예제 #1
0
        public async Task <IActionResult> Create(AboutCourse aboutCourse)
        {
            if (ModelState["File"].ValidationState == ModelValidationState.Invalid)
            {
                ModelState.AddModelError("", "Please choose image !");
                return(View());
            }

            if (!aboutCourse.File.IsImage())
            {
                ModelState.AddModelError("File", $"In this {aboutCourse.File.FileName} name file format not correct !");
                return(View());
            }

            if (aboutCourse.File.CheckFileSize(2000))
            {
                ModelState.AddModelError("File", $" In this {aboutCourse.File.FileName} name file size is greater than 150 kb !");
                return(View());
            }
            string folder = Path.Combine("img", "about");

            aboutCourse.Image = await aboutCourse.File.SaveFileAsync(_env.WebRootPath, folder);

            await _db.AboutCourses.AddAsync(aboutCourse);

            await _db.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
예제 #2
0
        public async Task <IActionResult> Update(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            AboutCourse about = await _db.AboutCourses.FirstOrDefaultAsync(c => c.IsDeleted == false && c.Id == id);

            if (about == null)
            {
                return(NotFound());
            }
            return(View(about));
        }
예제 #3
0
        public async Task <IActionResult> Update(int?id, AboutCourse aboutCourse)
        {
            if (id == null)
            {
                return(NotFound());
            }
            AboutCourse dbAboutCourse = await _db.AboutCourses.FirstOrDefaultAsync(c => c.IsDeleted == false && c.Id == id);

            if (dbAboutCourse == null)
            {
                return(NotFound());
            }

            if (ModelState["File"].ValidationState == ModelValidationState.Invalid)
            {
                ModelState.AddModelError("", "Please choose image !");
                return(View());
            }

            if (!aboutCourse.File.IsImage())
            {
                ModelState.AddModelError("File", $"In this {aboutCourse.File.FileName} name file format not correct !");
                return(View());
            }

            if (aboutCourse.File.CheckFileSize(2000))
            {
                ModelState.AddModelError("File", $" In this {aboutCourse.File.FileName} name file size is greater than 150 kb !");
                return(View());
            }

            string folder = Path.Combine("img", "about");

            Helper.DeleteFile(_env.WebRootPath, folder, dbAboutCourse.Image);

            dbAboutCourse.Heading     = aboutCourse.Heading;
            dbAboutCourse.Description = aboutCourse.Description;
            dbAboutCourse.Image       = await aboutCourse.File.SaveFileAsync(_env.WebRootPath, folder);

            await _db.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
예제 #4
0
        public async Task <IActionResult> DeleteAboutCourse(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            AboutCourse dbAboutCourse = await _db.AboutCourses.FirstOrDefaultAsync(c => c.IsDeleted == false && c.Id == id);

            if (dbAboutCourse == null)
            {
                return(NotFound());
            }
            string folder = Path.Combine("img", "about");

            Helper.DeleteFile(_env.WebRootPath, folder, dbAboutCourse.Image);

            dbAboutCourse.IsDeleted = true;
            await _db.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }