// GET: Profile public ActionResult MyProfile() { if (Session["EmailID"] != null) { var countries = db.Countries.Where(x => x.IsActive == true).ToList(); var genders = db.Genders.ToList(); var viewModel = new UserProfileCombine { Countries = countries, Genders = genders }; viewModel.Email = Session["EmailID"].ToString(); User user = db.Users.Where(x => x.EmailID.Equals(viewModel.Email)).FirstOrDefault(); viewModel.FirstName = user.FirstName; ViewBag.firstName = user.FirstName; viewModel.LastName = user.LastName; ViewBag.lastName = user.LastName; ViewBag.emailid = user.EmailID; UserProfile userProfile = new UserProfile(); userProfile.UserID = Convert.ToInt32(Session["ID"]); userProfile = db.UserProfiles.Where(x => x.UserID.Equals(userProfile.UserID)).FirstOrDefault(); viewModel.UserProfile = userProfile; return(View(viewModel)); } else { return(RedirectToAction("Login", "Account")); } }
public ActionResult MyProfile(UserProfileCombine user, HttpPostedFileBase ProfilePictureFile) { user.UserProfile.UserID = Convert.ToInt32(Session["ID"]); UserProfile userProfile = db.UserProfiles.Where(x => x.UserID == (user.UserProfile.UserID)).FirstOrDefault(); if (userProfile == null) { string ProfilePictureFileName; var userProfileDetail = new UserProfile() { AddressLine1 = user.UserProfile.AddressLine1, AddressLine2 = user.UserProfile.AddressLine2, City = user.UserProfile.City, College = user.UserProfile.College, Country = user.UserProfile.Country, CountryCode = user.UserProfile.CountryCode, CreatedDate = DateTime.Now, DOB = user.UserProfile.DOB, Gender = user.UserProfile.Gender, PhoneNumber = user.UserProfile.PhoneNumber, State = user.UserProfile.State, University = user.UserProfile.University, UserID = Convert.ToInt32(Session["ID"]), ZipCode = user.UserProfile.ZipCode }; try { db.UserProfiles.Add(userProfileDetail); db.SaveChanges(); if (ProfilePictureFile != null) { var fileExtension = Path.GetExtension(ProfilePictureFile.FileName); fileExtension = fileExtension.ToLower(); if (fileExtension == ".jpg" || fileExtension == ".jpeg" || fileExtension == ".png" || fileExtension == ".JPG" || fileExtension == ".JPEG" || fileExtension == ".PNG") { if (ProfilePictureFile.ContentLength < (10 * 1024 * 1024)) { ProfilePictureFileName = "DP_" + DateTime.Now.ToString("yyyyMMddhhmmss") + fileExtension; userProfileDetail.ProfilePicture = ProfilePictureFileName; ProfilePictureFileName = CheckifPathExistForCurrentUser() + ProfilePictureFileName; ProfilePictureFile.SaveAs(ProfilePictureFileName); db.SaveChanges(); } else { ModelState.AddModelError("ProfilePictureFile", "Please upload image file of max. 10 MB"); user.Genders = db.Genders.ToList(); user.Countries = db.Countries.Where(x => x.IsActive == true).ToList(); return(View(user)); } } else { ModelState.AddModelError("ProfilePictureFile", "Please upload image file of jpg, jpeg, png only"); user.Genders = db.Genders.ToList(); user.Countries = db.Countries.Where(x => x.IsActive == true).ToList(); return(View(user)); } } return(RedirectToAction("Index", "SearchNotes")); } catch (Exception) { throw; } } else { string ProfilePictureFileName; userProfile.AddressLine1 = user.UserProfile.AddressLine1; userProfile.AddressLine2 = user.UserProfile.AddressLine2; userProfile.City = user.UserProfile.City; userProfile.College = user.UserProfile.College; userProfile.Country = user.UserProfile.Country; userProfile.CountryCode = user.UserProfile.CountryCode; userProfile.CreatedDate = DateTime.Now; userProfile.DOB = user.UserProfile.DOB; userProfile.Gender = user.UserProfile.Gender; userProfile.PhoneNumber = user.UserProfile.PhoneNumber; userProfile.State = user.UserProfile.State; userProfile.University = user.UserProfile.University; userProfile.ZipCode = user.UserProfile.ZipCode; try { db.SaveChanges(); if (ProfilePictureFile != null && userProfile.ProfilePicture != null) { var fileExtension = Path.GetExtension(ProfilePictureFile.FileName); fileExtension = fileExtension.ToLower(); if (fileExtension == ".jpg" || fileExtension == ".jpeg" || fileExtension == ".png" || fileExtension == ".JPG" || fileExtension == ".JPEG" || fileExtension == ".PNG") { if (ProfilePictureFile.ContentLength < (10 * 1024 * 1024)) { ProfilePictureFileName = "DP_" + DateTime.Now.ToString("ddMMyyyyhhmmss") + fileExtension; var File = CheckifPathExistForCurrentUser() + userProfile.ProfilePicture; if (System.IO.File.Exists(File)) { System.IO.File.Delete(File); } userProfile.ProfilePicture = ProfilePictureFileName; ProfilePictureFileName = CheckifPathExistForCurrentUser() + ProfilePictureFileName; ProfilePictureFile.SaveAs(ProfilePictureFileName); db.SaveChanges(); } else { ModelState.AddModelError("ProfilePictureFile", "Please upload image file of max. 10 MB"); user.Genders = db.Genders.ToList(); user.Countries = db.Countries.Where(x => x.IsActive == true).ToList(); return(View(user)); } } else { ModelState.AddModelError("ProfilePictureFile", "Please upload image file of jpg, jpeg, png only"); user.Genders = db.Genders.ToList(); user.Countries = db.Countries.Where(x => x.IsActive == true).ToList(); return(View(user)); } } return(RedirectToAction("Index", "SearchNotes")); } catch (Exception) { throw; } } }