public ActionResult ClientIndex(string ClientName)
        {

            if (ModelState.IsValid)
            {
                if (ClientName != "")
                {
                    var Client = new Clients
                    {
                        ClientName = ClientName,
                        BuildingCount = 10,

                        //TO DO: update Clients table with matching fields from ClientsVM...
                    };
                    db.Clients.Add(Client);
                    db.SaveChanges();
                }
            }


            var clients = db.Clients

              .Select(c => new ClientsVM
              {
                  ID = c.ID,
                  ClientName = c.ClientName,
                  BuildingCount = (int)c.BuildingCount
              }).Take(10).ToList();
            return View(clients);

        }
        public ActionResult EditClient(Clients model)
        {

            if (ModelState.IsValid)
            {
                //var client = new Clients
                //{
                //     ID =model.ID,
                //     ClientName = model.ClientName,
                //     BuildingCount = model.BuildingCount
                //};
                model.BuildingCount = 0;
                db.Clients.Attach(model);
                var Entry = db.Entry(model);
                Entry.Property(c => c.ClientName).IsModified = true;
                Entry.Property(c => c.Address).IsModified = true;
                Entry.Property(c => c.City).IsModified = true;
                Entry.Property(c => c.State).IsModified = true;
                Entry.Property(c => c.Phone).IsModified = true;
                Entry.Property(c => c.Fax).IsModified = true;
                Entry.Property(c => c.Email ).IsModified = true;
                Entry.Property(c => c.BuildingCount).IsModified = true;

                db.SaveChanges();
            }
            return RedirectToAction("ClientIndex");
        }
        public async Task<ActionResult> deleteClient(Clients model)
        {

            if (ModelState.IsValid)
            {
                var client = await db.Clients.FindAsync(model.ID);
                db.Clients.Remove(client);
                await db.SaveChangesAsync();
            }


              
            return RedirectToAction("ClientIndex");
        }
        public ActionResult AddClient(ClientsVM model, int? buildingcount, int? ContactID)
        {




            try 
            {

                if (ModelState.IsValid)
                {

                    if (buildingcount == null)
                    {
                        //TO DO: this needs to be handle on while adding buildin. 
                        // cound should increase when adding building and decrease when deleting building
                        buildingcount = 0;
                    }
                    var newclient = new Clients
                    {
                        ClientName = model.ClientName,
                        BuildingCount = (int)buildingcount,
                        Address = model.Address,
                        City = model.city,
                        State = model.State,
                        ZipCode = model.zipcode,
                        Fax = model.Fax,
                        Phone = model.Phone,
                        Email = model.Email
                    };

                    db.Clients.Add(newclient);
                    db.SaveChanges();
                }
                else
                {
                    return RedirectToAction("clientIndex");
                    
                }
         } 
            catch(Exception e)
            {
                ViewBag.ErrorMessage = e.Message;
                return null;
            }

            return RedirectToAction("ClientIndex");
        }