public ActionResult Edit(EditViewModel model)
 {
     ApplicationDbContext db = new ApplicationDbContext();
     if (ModelState.IsValid)
     {
         try
         {
             var u_manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
             var currentUser = u_manager.FindById(model.UserId);
             if (currentUser != null)
             {
                 string userFullName = DataHelper.ToPascalConvention(model.FirstName + " " + model.LastName);
                 currentUser.UserName = model.Email;
                 currentUser.FirstName = model.FirstName;
                 currentUser.LastName = model.LastName;
                 currentUser.FullName = userFullName;
                 currentUser.PhoneNumber = model.ContactNo;
                 currentUser.Email = model.Email;
                 //  currentUser.PasswordHash = Crypto.HashPassword(model.Password);                      
                 db.Entry(currentUser).State = EntityState.Modified;
                 db.SaveChanges();
                 
                 var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
                 var role = roleManager.FindById(model.RoleId);
                 if (role != null)
                 {
                     u_manager.RemoveFromRole(model.UserId, model.RoleName);
                     u_manager.AddToRole(model.UserId, role.Name);
                     db.SaveChanges();
                 }
                 List<Product> listProducts = new List<Product>();
                 int userProductId = DBHandler.UserProductId(model.UserId);
                 ManagerProduct objManagerProduct = new ManagerProduct();
                   
                 if (userProductId > 0)
                 {                          
                                  
                     objManagerProduct = db.ManagerProducts.Find(userProductId);
                     objManagerProduct.ProductIds = model.selectedProduct;         
                     objManagerProduct.ApplicationUserId = objManagerProduct.ApplicationUserId;
                     objManagerProduct.UpdatedOn = DateTime.Now;
                     objManagerProduct.CreatedOn = objManagerProduct.CreatedOn;
                     db.Entry(objManagerProduct).State = EntityState.Modified;
                     db.SaveChanges();  
                 }
                 else
                 {
                     objManagerProduct.UpdatedOn = DateTime.Now;
                     objManagerProduct.ProductIds = model.selectedProduct;         
                     objManagerProduct.ApplicationUserId = model.UserId;
                     objManagerProduct.CreatedOn = DateTime.Now;
                     db.ManagerProducts.Add(objManagerProduct);
                     db.SaveChanges();
                 }
                
                 DBHandler.SaveUserLogged(currentUser, model.RoleId, role.Name, UserLoggedActions.UserUpadated);
                 return RedirectToAction("Index", "User");
             }
         }
         catch (Exception)
         {
             throw;
         }
     }
     ViewBag.RoleId = new SelectList(db.Roles, "Id", "Name", model.RoleId);
     return View(model);
 }
 public ActionResult Edit(string id)
 {
     ApplicationDbContext db = new ApplicationDbContext();
     if (id == null)
     {
         return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
     }
     var u_manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
     string role_name = u_manager.GetRoles(id).FirstOrDefault() == null ? "" : u_manager.GetRoles(id).FirstOrDefault();
     ApplicationUser applicationUser = db.Users.Find(id);
     EditViewModel appUser = new EditViewModel();
     appUser.ContactNo = applicationUser.PhoneNumber;
     appUser.Email = applicationUser.Email;
     appUser.FirstName = DataHelper.ToPascalConvention(applicationUser.FirstName);
     appUser.LastName = DataHelper.ToPascalConvention(applicationUser.LastName);
     appUser.Password = applicationUser.Password;
     appUser.RoleName = role_name;
     appUser.UserId = applicationUser.Id;
     appUser.ConfirmPassword = applicationUser.Password;           
     appUser.selectedProduct = DBHandler.getUserProductsByUserId(applicationUser.Id);
     appUser.Products = DBHandler.GetProducts();
     appUser.RoleId = db.Roles.Where(r => r.Name.ToLower() == role_name.ToLower()).First().Id;
     ViewBag.RoleId = new SelectList(db.Roles, "Id", "Name", appUser.RoleId);
     if (appUser == null)
     {
         return HttpNotFound();
     }
     return View(appUser);
 }