public ActionResult Edit(EditUserViewModel user) { if(!ModelState.IsValid) { var results = ModelState .Where(m => m.Value.Errors.Count > 0) .Select(m => new { Field = m.Key, Message = m.Value.Errors[0].ErrorMessage }).ToArray(); //ModelState.AddModelError("", "error message"); Response.StatusCode = 400; return Json(results); } SaveUser(user); Response.StatusCode = 200; return Json("OK"); }
private void SaveUser(EditUserViewModel user) { UserModel dbUser = usersRepository.GetByID(user.UserId); dbUser.FirstName = user.FirstName; dbUser.LastName = user.LastName; dbUser.Email = user.Email; usersRepository.Save(); }
public ActionResult Manage(EditUserViewModel user) { UserModel currentUser = GetCurrentUser(); if (!currentUser.IsAdmin && currentUser.UserId != user.UserId) { return RedirectToAction("index", "home"); } if (!ModelState.IsValid) { return Manage(user.UserId); } SaveUser(user); return RedirectToAction("index", "home"); }