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 async Task<ActionResult> Register(RegisterViewModel model)
 {
     ApplicationDbContext db = new ApplicationDbContext();
     if (ModelState.IsValid)
     {
         string userFullName = DataHelper.ToPascalConvention(model.FirstName + " " + model.LastName);
         var user = new ApplicationUser { LoginCounter = 0, UserName = model.Email, FullName = userFullName, FirstName = model.FirstName, Password = model.Password, LastName = model.LastName, IsActive = true, Email = model.Email, PhoneNumber = model.ContactNo, PasswordHash = model.Password };
         var result = await UserManager.CreateAsync(user, model.Password);
         if (result.Succeeded)
         {
             List<Product> listProducts = new List<Product>();
             if (!string.IsNullOrEmpty(model.selectedProduct))
             {  
                     ManagerProduct objManagerProduct = new ManagerProduct();
                     objManagerProduct.ProductIds = model.selectedProduct;
                     string currentAddedUserId = DBHandler.GetUserByEmail(model.Email).Id;
                     objManagerProduct.ApplicationUserId = currentAddedUserId;
                     objManagerProduct.CreatedOn = DateTime.Now;
                     objManagerProduct.UpdatedOn= DateTime.Now;
                     db.ManagerProducts.Add(objManagerProduct);
                     db.SaveChanges();
                 
             }
             var role = db.Roles.Find(model.RoleId);
             if (role != null)
             {
                 UserManager.AddToRole(user.Id, role.Name);
             }
             DBHandler.SaveUserLogged(user, model.RoleId, role.Name, UserLoggedActions.UserRegisteredRoleAssigned);
             return RedirectToAction("Index", "User");
         }
         AddErrors(result);
     }
     ViewBag.RoleId = new SelectList(db.Roles, "Id", "Name", model.RoleId);
     return View(model);
 }