Example #1
0
        public async Task <IActionResult> Edit(int id, TeachersImageDemoModel model)
        {
            if (id != model.Teacher.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(model.Teacher);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TeacherExists(model.Teacher.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model.Teacher));
        }
Example #2
0
        private string UploadedFile(TeachersImageDemoModel model)
        {
            string uniqueFileName = null;

            if (model.ProfileImage != null)
            {
                string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "images");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(model.ProfileImage.FileName);
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    model.ProfileImage.CopyTo(fileStream);
                }
            }
            return(uniqueFileName);
        }
Example #3
0
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var teacher = await _context.Teacher.FindAsync(id);

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

            var viewmodel = new TeachersImageDemoModel
            {
                Teacher = teacher
            };


            return(View(viewmodel));
        }
Example #4
0
        public async Task <IActionResult> Create(TeachersImageDemoModel model)
        {
            if (ModelState.IsValid)
            {
                string uniqueFileName = UploadedFile(model);

                Teacher teacher = new Teacher
                {
                    FirstName      = model.Teacher.FirstName,
                    LastName       = model.Teacher.LastName,
                    Degree         = model.Teacher.Degree,
                    AcademicRank   = model.Teacher.AcademicRank,
                    OfficeNumber   = model.Teacher.OfficeNumber,
                    HireDate       = model.Teacher.HireDate,
                    ProfilePicture = uniqueFileName,
                };

                _context.Teacher.Add(teacher);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View());
        }