public ActionResult Edit() { var userId = User.Identity.GetUserId(); var user = context.Users.SingleOrDefault(u => u.Id == userId); if (user == null) { return(HttpNotFound()); } var viewModel = new PorfileViewModel(); viewModel.Firstname = user.Firstname; if (user.EducationId != null) { viewModel.Education = context.Educations.SingleOrDefault(e => e.Id == user.EducationId).Name; } //{ // Firstname = user.Firstname, // Educations = context.Educations.ToList(), // Education = context.Educations.SingleOrDefault(e => e.Id == user.EducationId).Name //}; return(View("UserForm", viewModel)); }
public ActionResult Edit(PorfileViewModel porfileViewModel) { var education = context.Educations.SingleOrDefault(e => e.Name == porfileViewModel.Education); if (education == null && !String.IsNullOrEmpty(porfileViewModel.Education)) { education = new Education() { Name = porfileViewModel.Education }; context.Educations.Add(education); context.SaveChanges(); } context.Educations.SingleOrDefault(e => e.Name == porfileViewModel.Education); if (!ModelState.IsValid) { return(RedirectToAction("MyProfile")); } var x = context.Educations.SingleOrDefault(e => e.Name == porfileViewModel.Education); var userId = User.Identity.GetUserId(); var userInDb = context.Users.Single(u => u.Id == userId); userInDb.Firstname = porfileViewModel.Firstname; if (!String.IsNullOrEmpty(porfileViewModel.Education)) { userInDb.EducationId = context.Educations.SingleOrDefault(e => e.Name == porfileViewModel.Education).Id; } else { userInDb.EducationId = null; } context.SaveChanges(); return(RedirectToAction("MyProfile")); }