Exemplo n.º 1
0
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var teacher = await _context.Teachers.Where(t => t.Id == id).FirstOrDefaultAsync();

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

            string imagePath = null;

            if (teacher.CoverPhoto != null)
            {
                imagePath = GetFilePathShow(teacher.CoverPhoto);
            }

            var model = new TeacherEditBindingModel
            {
                Id                    = teacher.Id,
                Description           = teacher.Description.Replace("<br />", Environment.NewLine),
                Name                  = teacher.Name,
                ImagePath             = GetFilePathShow(teacher.CoverPhoto),
                ImageFileFromDatabase = teacher.CoverPhoto
            };

            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Edit(int id, TeacherEditBindingModel teacher)
        {
            if (id != teacher.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    string filePath = null;

                    //upload image
                    if (teacher.ImageFile != null && teacher.ImageFile.Length > 0)
                    {
                        filePath = await UploadImageAsync(teacher.ImageFile);

                        if (filePath == null)
                        {
                            ModelState.AddModelError("ImageFile", "Invalid File Type!");
                            return(View());//todo
                        }
                    }
                    else
                    {
                        filePath = teacher.ImageFileFromDatabase;
                    }


                    Teacher teacherToUpdate = new Teacher
                    {
                        Id          = teacher.Id,
                        Name        = teacher.Name,
                        Description = teacher.Description.Replace("\r\n", "<br />").Replace("\n", "<br />"),
                        CoverPhoto  = filePath
                    };

                    _context.Update(teacherToUpdate);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PostExists(teacher.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(teacher));
        }