public ActionResult SaveTutorInfo(HttpPostedFileBase Avatar, PrivateTutorOnline.Models.BindingModels.TutorBindingModel tutorInfo) { if (Avatar != null && Avatar.ContentLength > 0) { // extract only the fielname var fileName = Path.GetFileName(Avatar.FileName); // store the file inside ~/App_Data/uploads folder var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName); Avatar.SaveAs(path); Tutor tutor = new Tutor(); tutor.FullName = tutorInfo.FullName; tutor.Address = tutorInfo.Address; tutor.Advantage = tutorInfo.Advantage; tutor.DateOfBirth = tutorInfo.DateOfBirth; tutor.Gender = tutorInfo.Gender; tutor.Degree = tutorInfo.Degree; tutor.Email = tutorInfo.Email; tutor.GraduationYear = tutorInfo.GraduationYear; tutor.HomeTown = tutorInfo.HomeTown; tutor.IdentityNumber = tutorInfo.IdentityNumber; tutor.MajorSubject = tutorInfo.MajorSubject; tutor.PhoneNumber = tutorInfo.PhoneNumber; tutor.University = tutorInfo.UniversityName; tutor.Image = new byte[Avatar.ContentLength]; Avatar.InputStream.Read(tutor.Image, 0, Avatar.ContentLength); tutor.Subjects = new List <Subject>(); tutor.Grades = new List <Grade>(); foreach (int i in tutorInfo.Subjects) { tutor.Subjects.Add(db.Subjects.SingleOrDefault(s => s.Id == i)); } foreach (int i in tutorInfo.Grades) { tutor.Grades.Add(db.Grades.SingleOrDefault(gr => gr.Id == i)); } db.Tutors.Add(tutor); db.SaveChanges(); return(View("Success")); } else { return(View("Error")); } }
public ActionResult EditTutorInfo(HttpPostedFileBase Avatar, PrivateTutorOnline.Models.BindingModels.TutorBindingModel tutorInfo) { Tutor tutor = db.Tutors.Include(s => s.Grades).Include(s => s.Subjects).SingleOrDefault(s => s.Id == tutorInfo.Id); tutor.Subjects.Clear(); tutor.Grades.Clear(); db.Entry(tutor).State = EntityState.Modified; db.SaveChanges(); tutor = db.Tutors.Include(s => s.Grades).Include(s => s.Subjects).SingleOrDefault(s => s.Id == tutorInfo.Id); if (tutor != null) { if (Avatar != null) { tutor.Image = new byte[Avatar.ContentLength]; Avatar.InputStream.Read(tutor.Image, 0, Avatar.ContentLength); } tutor.FullName = tutorInfo.FullName; tutor.City = tutorInfo.City; tutor.District = tutorInfo.District; tutor.Ward = tutorInfo.Ward; tutor.Street = tutorInfo.Street; tutor.Advantage = tutorInfo.Advantage; if (tutorInfo.DateOfBirth.HasValue) { tutor.DateOfBirth = tutorInfo.DateOfBirth.Value; } tutor.Gender = tutorInfo.Gender; tutor.Degree = tutorInfo.Degree; tutor.Email = tutorInfo.Email; tutor.GraduationYear = tutorInfo.GraduationYear; tutor.HomeTown = tutorInfo.HomeTown; tutor.IdentityNumber = tutorInfo.IdentityNumber; tutor.MajorSubject = tutorInfo.MajorSubject; tutor.PhoneNumber = tutorInfo.PhoneNumber; tutor.University = tutorInfo.UniversityName; if (tutorInfo.Subjects.Count > 0) { tutor.Subjects = new List <Subject>(); foreach (int i in tutorInfo.Subjects) { tutor.Subjects.Add(db.Subjects.SingleOrDefault(s => s.Id == i)); } } if (tutorInfo.Grades.Count > 0) { tutor.Grades = new List <Grade>(); foreach (int i in tutorInfo.Grades) { tutor.Grades.Add(db.Grades.SingleOrDefault(gr => gr.Id == i)); } } UserManager.SetEmail(User.Identity.GetUserId(), tutor.Email); db.Entry(tutor).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index", "Manage")); } else { return(View("Error")); } }