public async Task <IActionResult> Edit(string id, [Bind("Username,Password,FirstName,LastName,Email,Gender,BirthDate,Address,City,ProvinceId,PostalCode,SecretQuestion1,SecretAnswer1,SecretQuestion2,SecretAnswer2,DisplayUrl,Role,PhoneNumber,PaymentId,PropertyId")] User user) { if (id != user.Username) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(user); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserExists(user.Username)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(user)); }
public async Task <IActionResult> Edit(User user) { // check valition from model data annotation if (ModelState.IsValid) { try { Log.Information("Save user edit to DB"); _context.Update(user); await _context.SaveChangesAsync(); TempData["Success"] = "Success"; return(RedirectToAction("Account", "Home")); } catch (Exception ex) { Log.ForContext <UserController>().Error(ex, "Error with saving to DB"); TempData["Error"] = "Error with server, please contact customer support"; return(View("Edit", user)); } } else { Log.Warning("Model not valid"); TempData["Error"] = "Please check fields"; return(View("Edit", user)); } }
public async Task <IActionResult> Register(User user, UserProperty userProperty, IFormFile file, Payment payment) { Log.Information("Registering user..."); // run if modal is valid using data annotation built in model try { // set role to User, admin can only be added through SQL user.Role = "User"; _context.Add(user); await _context.SaveChangesAsync(); if (payment.Number != null) { _context.Add(payment); await _context.SaveChangesAsync(); } if (userProperty.PropAddress != null) { if (file != null) { Log.Information("Convert image to format for DB"); using (var ms = new MemoryStream()) { file.CopyTo(ms); userProperty.ImageUrl = ms.ToArray(); } } _context.Add(userProperty); await _context.SaveChangesAsync(); } user.PropertyId = userProperty.PropertyId; user.PaymentId = payment.PaymentId; _context.Update(user); await _context.SaveChangesAsync(); TempData["Success"] = "Account successfully created!"; return(RedirectToAction(nameof(Index))); } catch (Exception ex) { Log.ForContext <HomeController>().Error(ex, "Problem with registration for {0}", user.Username); } return(View(user)); }