public IActionResult CustomerEdit(string AddressId) { Address userAddress = _addressService.Query(x => x.AddressId.ToString() == AddressId).FirstOrDefault(); AddressCustomerEditViewModel VM = new AddressCustomerEditViewModel { StreetAddress = userAddress.StreetAddress == null ? "" : _textFormatter.RemoveDoubleSpaces (_textFormatter.CapitaliseFirstLetters(userAddress.StreetAddress, false)), Suburb = userAddress.Suburb == null ? "" : _textFormatter.RemoveDoubleSpaces (_textFormatter.CapitaliseFirstLetters(userAddress.Suburb, false)), State = userAddress.State == null ? "" : _textFormatter.RemoveDoubleSpaces (_textFormatter.CapitaliseFirstLetters(userAddress.State, false)), Postcode = userAddress.Postcode, AddressType = userAddress.AddressType, PreferredShippingAddress = userAddress.PreferredShippingAddress }; TempData["AddressId"] = AddressId.ToString(); return(View(VM)); }
public IActionResult CustomerEdit(AddressCustomerEditViewModel VM) { if (ModelState.IsValid) { string AddressIdTemp; if (TempData["AddressId"] != null) { AddressIdTemp = TempData["AddressId"].ToString(); } else { return(RedirectToAction("ManageProfile", "Account")); } // If this address has been selected as the preferred, set all others from this user to false. if (VM.PreferredShippingAddress) { string UserName = User.Identity.Name; ApplicationUser user = _userManager.Users.FirstOrDefault(x => x.UserName == UserName); if (_addressService.Query(x => x.ApplicationUserId == user.Id) != null) { IEnumerable <Address> userAddresses = _addressService.Query(x => x.ApplicationUserId == user.Id).ToList(); foreach (var address in userAddresses) { address.PreferredShippingAddress = false; _addressService.Update(address); } } } Address userAddress = _addressService.Query(x => x.AddressId.ToString() == AddressIdTemp).FirstOrDefault(); // Converting VM to Model: userAddress.StreetAddress = _textFormatter.RemoveDoubleSpaces (_textFormatter.CapitaliseFirstLetters(VM.StreetAddress, false)); userAddress.Suburb = _textFormatter.RemoveDoubleSpaces (_textFormatter.CapitaliseFirstLetters(VM.Suburb, false)); userAddress.State = _textFormatter.RemoveDoubleSpaces (_textFormatter.CapitaliseFirstLetters(VM.State, false)); userAddress.Postcode = VM.Postcode; userAddress.AddressType = VM.AddressType; userAddress.PreferredShippingAddress = VM.PreferredShippingAddress; // Updating address: _addressService.Update(userAddress); return(RedirectToAction("ManageProfile", "Account")); } return(View(VM)); }