public async Task <ActionResult> CreateEdit(Roles model) { string returnId = ""; try { if (model.Id != null) { var user = oDB.AspNetRoles.Where(m => m.Id == model.Id).FirstOrDefault(); user.Name = model.Name; oDB.Entry(user).State = System.Data.Entity.EntityState.Modified; oDB.SaveChanges(); returnId = model.Id; } else { Guid IdGen = Guid.NewGuid(); var user = new WebApplication1.Models.AspNetRole { Id = IdGen.ToString(), Name = model.Name, }; oDB.AspNetRoles.Add(user); oDB.SaveChanges(); } return(Json(new { returnId = returnId, }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { } ViewBag.Action = (model.Id != null) ? "Edit" : "Create"; return(Json(new { returnId = returnId, }, JsonRequestBehavior.AllowGet)); }
public async Task <ActionResult> CreateEdit(Users model) { // RequestResultModel requestModel = new RequestResultModel(); string returnId = ""; try { model.CreatedDate = DateTime.UtcNow; model.ModifiedDate = DateTime.UtcNow; if (model.Ids != null) { ViewBag.LoadRoles = (new UserDB()).LoadRoles(); var user = oDB.AspNetUsers.Where(m => m.Id == model.Ids).FirstOrDefault(); user.Name = model.Name; user.UserName = model.UserName; user.CompanyEmail = model.CompanyEmail; user.Address = model.Address; user.City = model.City; user.Zip = model.Zip; user.PhoneNumber = model.Contact; user.Notes = model.Notes; user.isActive = true; oDB.Entry(user).State = System.Data.Entity.EntityState.Modified; oDB.SaveChanges(); returnId = model.Ids; } else { ViewBag.LoadRoles = (new UserDB()).LoadRoles(); var user = new ApplicationUser { UserName = model.UserName, Email = model.Email, Name = model.Name, CompanyEmail = model.Email, Address = model.Address, City = model.City, CreatedDate = model.CreatedDate.Value, Zip = model.Zip, PhoneNumber = model.Contact, Notes = model.Notes, isActive = true, }; var result = UserManager.Create(user, model.Password); if (result.Succeeded) { var currentUser = UserManager.FindByName(user.UserName); var roleresult = UserManager.AddToRole(currentUser.Id, model.RoleName); // returnId = currentUser.Id; // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 } // oDB.tblUsers.Add(user); // oDB.SaveChanges(); } return(Json(new { returnId = returnId, }, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { } ViewBag.Action = (model.Id > 0) ? "Edit" : "Create"; return(Json(new { returnId = returnId, }, JsonRequestBehavior.AllowGet)); }