public IActionResult Edit() { var user = _usersService.GetUser(new UserKey() { Id = SessionUserId }); if (user != null) { var userInfo = _userInfosService.GetUserInfo(new UserInfoKey() { UserId = SessionUserId }); var viewModel = new UserProfilViewModel(); viewModel.UserId = user.Id; viewModel.Name = user.Name; viewModel.Email = user.Email; if (userInfo != null) { viewModel.SexId = (int)userInfo.Sex; viewModel.Unit = (int)userInfo.Unit; viewModel.Height = userInfo.Height; viewModel.Weight = userInfo.Weight; viewModel.ZipCode = userInfo.ZipCode; viewModel.CountryId = userInfo.CountryId; viewModel.TimeZoneName = userInfo.TimeZoneName; viewModel.ImageUrl = ImageUtils.GetImageUserProfileRelativeURL(user, _env); } ViewBag.Sex = ControllerUtils.CreateSelectSexItemList(viewModel.SexId); ViewBag.Countries = ControllerUtils.CreateSelectCountryItemList(_countriesService.FindCountries(), viewModel.CountryId); ViewBag.Units = ControllerUtils.CreateSelectUnitItemList(viewModel.Unit); ViewBag.TimeZones = ControllerUtils.CreateSelectTimeZoneItemList(viewModel.TimeZoneName); return(View(viewModel)); } return(RedirectToAction("Index")); }
public IActionResult Edit(UserProfilViewModel viewModel, IFormFile imageFile) { int sexId = 0, countryId = 0; if (ModelState.IsValid && _signInManager.IsSignedIn(User) && viewModel != null) { if (viewModel.UserId == SessionUserId) { if (viewModel.CountryId == 0) // not specified { viewModel.ZipCode = string.Empty; } bool continute = true; if (sexId < 0 && sexId > 1) { ModelState.AddModelError(string.Empty, string.Format("{0} {1}", Translation.INVALID_INPUT_2P, Translation.SEX)); viewModel.SexId = 0; continute = false; } if (continute && viewModel.CountryId != 0) { var country = _countriesService.GetCountry(new CountryKey() { Id = viewModel.CountryId }); if (country == null) { ModelState.AddModelError(string.Empty, string.Format("{0} {1}", Translation.INVALID_INPUT_2P, Translation.COUNTRY)); viewModel.CountryId = 0; continute = false; } } if (continute && !string.IsNullOrEmpty(viewModel.ZipCode)) { // ZipCode not Required var city = _citiesService.GetCity(new CityKey() { CountryId = viewModel.CountryId, ZipCode = viewModel.ZipCode }); if (city == null) { ModelState.AddModelError(string.Empty, string.Format("{0} {1}", Translation.INVALID_INPUT_2P, Translation.ZIP_CODE)); continute = false; } } sexId = viewModel.SexId; countryId = viewModel.CountryId; if (continute) { var userInfo = new UserInfo() { UserId = viewModel.UserId, Unit = (TUnitType)viewModel.Unit, Height = viewModel.Height, Weight = viewModel.Weight, ZipCode = viewModel.ZipCode, CountryId = viewModel.CountryId, Sex = (TSexType)viewModel.SexId, TimeZoneName = viewModel.TimeZoneName, }; userInfo = _userInfosService.UpdateUserInfo(userInfo); if (!string.IsNullOrWhiteSpace(userInfo.UserId) && ImageUtils.CheckUploadedImageIsCorrect(imageFile)) { string ext = ImageUtils.GetImageExtension(imageFile); if (string.IsNullOrWhiteSpace(ext)) { return(BadRequest()); } ImageUtils.SaveImage(imageFile, Path.Combine(_env.WebRootPath, "images", "userprofil"), userInfo.UserId + ext); } return(RedirectToAction("Index")); } } } ViewBag.Sex = ControllerUtils.CreateSelectSexItemList(sexId); ViewBag.Countries = ControllerUtils.CreateSelectCountryItemList(_countriesService.FindCountries(), countryId); ViewBag.Units = ControllerUtils.CreateSelectUnitItemList(viewModel.Unit); ViewBag.TimeZones = ControllerUtils.CreateSelectTimeZoneItemList(viewModel.TimeZoneName); return(View(viewModel)); }