public ActionResult ChangeProfile() { var userId = User.Identity.GetUserId(); ApplicationUser applicationUser = db.Users.Find(userId); if (applicationUser == null) { return(HttpNotFound()); } // return View(applicationUser); var model = new ChangeProfileViewModels(); model.GivenName = applicationUser.GivenName; model.FamilyName = applicationUser.FamilyName; model.Email = applicationUser.Email; model.ProfileImageRef = applicationUser.ProfileImageRef; model.PhoneNumber = applicationUser.PhoneNumber; return(View(model)); }
public ActionResult Edit(string id) { ApplicationUser applicationUser = db.Users.Find(id); if (applicationUser == null) { return(HttpNotFound()); } // return View(applicationUser); var model = new ChangeProfileViewModels(); model.UserId = id; model.GivenName = applicationUser.GivenName; model.FamilyName = applicationUser.FamilyName; model.Email = applicationUser.Email; model.ProfileImageRef = applicationUser.ProfileImageRef; model.PhoneNumber = applicationUser.PhoneNumber; return(View(model)); }
public ActionResult Edit(ChangeProfileViewModels model) { if (!ModelState.IsValid) { return(View(model)); } ApplicationUser dbAU = db.Users.Find(model.UserId); dbAU.GivenName = model.GivenName; dbAU.FamilyName = model.FamilyName; var oldEmail = dbAU.Email; if (Request.Files.Count > 0) { string base64 = Request.Form["imgCropped"]; if (base64 != null && base64 != "") { byte[] bytes = Convert.FromBase64String(base64.Split(',')[1]); HttpPostedFileBase file = Request.Files[0]; Random rnd = new Random(); var a = rnd.Next(100000); using (FileStream stream = new FileStream(Server.MapPath("~/Pictures/" + a + "-" + file.FileName), FileMode.Create)) { stream.Write(bytes, 0, bytes.Length); stream.Flush(); dbAU.ProfileImageRef = +a + "-" + file.FileName; } } //HttpPostedFileBase file = Request.Files[0]; //if (file.ContentLength > 0) //{ // file.SaveAs(HttpContext.Server.MapPath("~/Pictures/") // + file.FileName); // dbAU.ProfileImageRef = file.FileName; //} } if (db.Users.Any(u => u.UserName == model.Email) && dbAU.UserName != model.Email) { ViewBag.Errmsg = "This email is existed in the database, try another one!"; return(View(model)); } if (model.Email == "" || model.Email == null) { ViewBag.Errmsg = "You must type an valid email address here!"; return(View(model)); } dbAU.Email = model.Email; dbAU.PhoneNumber = model.PhoneNumber; dbAU.UserName = model.Email; db.Entry(dbAU).State = EntityState.Modified; db.SaveChanges(); if (oldEmail != model.Email) { Request.GetOwinContext().Authentication.SignOut(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ApplicationCookie); Session.Abandon(); return(RedirectToAction("RedirectToPage", "ApplicationUser")); } return(RedirectToAction("Index")); }