public ActionResult Create(User user) { if (ModelState.IsValid) { db.User.Add(user); db.SaveChanges(); return RedirectToAction("Index"); } return View(user); }
public ActionResult Edit(User user) { if (ModelState.IsValid) { int roleId = Convert.ToInt32(Request.Form["roleId"]); var role = db.Role.Where(r => r.Id == roleId).FirstOrDefault(); var userIds = db.User.Include(c => c.Role).Where(r => r.Id == user.Id).FirstOrDefault(); userIds.FullName = user.FullName; userIds.UserName = user.UserName; userIds.IsAdministrator = user.IsAdministrator; userIds.Status = user.Status; userIds.Email = user.Email; if (role != null) { userIds.Role.Clear(); userIds.Role.Add(role); } db.Entry(userIds).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(user); }