public async Task <IActionResult> Create(StudentFormVM Vmodel) { if (ModelState.IsValid) { string uniqueFileName = UploadedFile(Vmodel); Student student = new Student { ProfilePicture = uniqueFileName, StudentId = Vmodel.StudentId, FirstName = Vmodel.FirstName, LastName = Vmodel.LastName, EnrollmentDate = Vmodel.EnrollmentDate, AcquiredCredits = Vmodel.AcquiredCredits, CurrentSemestar = Vmodel.CurrentSemestar, EducationLevel = Vmodel.EducationLevel, Courses = Vmodel.Courses, }; _context.Add(student); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View()); }
// GET: Students/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var student = await _context.Student.FindAsync(id); if (student == null) { return(NotFound()); } StudentFormVM Vmodel = new StudentFormVM { Id = student.Id, FirstName = student.FirstName, LastName = student.LastName, StudentId = student.StudentId, EnrollmentDate = student.EnrollmentDate, AcquiredCredits = student.AcquiredCredits, CurrentSemestar = student.CurrentSemestar, EducationLevel = student.EducationLevel, Courses = student.Courses }; ViewData["StudentFullName"] = _context.Student.Where(t => t.Id == id).Select(t => t.FullName).FirstOrDefault(); return(View(Vmodel)); }
private string UploadedFile(StudentFormVM model) { string uniqueFileName = null; if (model.ProfilePicture != null) { string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(model.ProfilePicture.FileName); string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { model.ProfilePicture.CopyTo(fileStream); } } return(uniqueFileName); }
public async Task <IActionResult> Edit(int id, StudentFormVM Vmodel) { if (id != Vmodel.Id) { return(NotFound()); } if (ModelState.IsValid) { try { string uniqueFileName = UploadedFile(Vmodel); Student student = new Student { Id = Vmodel.Id, FirstName = Vmodel.FirstName, LastName = Vmodel.LastName, ProfilePicture = uniqueFileName, EnrollmentDate = Vmodel.EnrollmentDate, CurrentSemestar = Vmodel.CurrentSemestar, AcquiredCredits = Vmodel.AcquiredCredits, StudentId = Vmodel.StudentId, EducationLevel = Vmodel.EducationLevel, Courses = Vmodel.Courses }; _context.Update(student); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!StudentExists(Vmodel.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(Vmodel)); }