public async Task<ActionResult> AddManagerTobuilding(ManagementBuilding model, ManagerVM  model2)
        {

            ViewBag.ClientID = db.Buildings.Where(b => b.ID == model.buildingID).FirstOrDefault().Clients.ID;
            ViewBag.BuildingName = db.Buildings.Where(b => b.ID == model.buildingID).FirstOrDefault().BuildingName;

            try
            {
            if (!ModelState.IsValid)
            {
                return View("ManagementBuilding", model);
            }
            ApplicationDbContext context = new ApplicationDbContext();

            var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            PasswordHasher hasher = new PasswordHasher();
            var a = UserManager.FindByEmail(model2.Email);
            if (a != null)
            {
                return View("ManagementBuilding", model);
            }
            ApplicationUser AppUser = new ApplicationUser()
            {
                Id = Guid.NewGuid().ToString(),
                Email = model2.Email,
                UserName = model2.Username,
                SecurityStamp = Guid.NewGuid().ToString(),
                PhoneNumber = model2.Phone,
                LockoutEnabled = false,
                LockoutEndDateUtc= DateTime.Now.AddDays(365),
                AccessFailedCount = 0,
                PhoneNumberConfirmed = false,
                TwoFactorEnabled = false,
                EmailConfirmed = false,
                PasswordHash = hasher.HashPassword(model2.Password)
            };
            string[] FullName = model2.FullName.Split(new string[] { " " }, StringSplitOptions.None);
            Manager mgr = new Manager()
            {
                ID = AppUser.Id,
                FirstName = FullName[0].ToString(),
                LastName = FullName[1].ToString(),
                Phone = model2.Phone,
                ClientID = model2.clientID
            };
            db.Manager.Add(mgr);
            context.Users.Add(AppUser);

            await context.SaveChangesAsync();
            await db.SaveChangesAsync();

            RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
            if (!RoleManager.RoleExists("Manager"))
            {var roleresult = RoleManager.Create(new IdentityRole("Manager"));}
            var Result = UserManager.AddToRole(AppUser.Id, "Manager");

            ManagerBuilding ObjManagerBuilding = new ManagerBuilding()
            {
                 BuildingID = model2.BuildingID,
                  ManagerID = mgr.ID ,
                   UserID =mgr.ID
            };

            db.ManagerBuilding.Add(ObjManagerBuilding);
            await db.SaveChangesAsync();
         

            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
            return RedirectToAction("ManagementBuilding", new { BuildingID=model2.BuildingID});
        }        
       public ActionResult ManagerBuilding(DisplayClientBuilding model, int BuildingID)
        {
            //1. set ManagerBuilding Obj and save it in db
            //2. load clients, Buildings, assigned buildings to the current manager
            //3.
            model.Manager = db.Manager.Find(model.ManagerID);
            ManagerBuilding ObjMB = new ManagerBuilding
            {
                BuildingID =BuildingID,
                UserID = model.ManagerID,
                 ManagerID = model.ManagerID
            };
            if (db.ManagerBuilding.Where(c => c.UserID == model.ManagerID && c.BuildingID == BuildingID).FirstOrDefault() == null)
            {
                db.ManagerBuilding.Add(ObjMB);
                db.SaveChanges();
            }
            //get All clients
            model.clients = db.Clients.Where(c => c.ID == model.ClientID).ToList();
            //get all entries that belong to the current Manager on the managerbuilding table
            model.ManagerBuildings = db.ManagerBuilding.Where(c => c.UserID == model.ManagerID).ToList();
            //selected client's building
            var selectedclitentbuildings = db.Buildings
                .Where(c => c.Clients.ID == model.ClientID).ToList();

            model.buildings = db.Buildings.Where(c=>c.ClientID==model.ClientID).ToList();
            if (selectedclitentbuildings != null)
            {
                foreach(var item in model.ManagerBuildings )
                {    //building that exist on managerbuilding deck
                     Buildings removeit =db
                    .Buildings
                    .First(c=>c.ID == @item.BuildingID);
                    //note: current user records
                     model.buildings.Remove(removeit);
                     model.BuildingsOnDeck.Add(removeit);
                } 
            }
            
            return View("SelectBuilding",model);
        }