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 JsonResult EditClientAjax(Clients model)
        {

            if (model != null)
            {               
                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;
                Entry.Property(c => c.ZipCode).IsModified = true;

                db.SaveChanges();

            }


            var clientList = db.Clients.Select(c => new
            {
                ClientName = c.ClientName,
                ClientPhone = c.Phone,
                ClientFullAddress = c.Address + " " + c.City + " " + c.State + " " + c.ZipCode,
                ClientEmail = c.Email,
                ClientFax = c.Fax,
                ClientAddress = c.Address,
                ClientCity = c.City,
                ClientState = c.State,
                ClientZipcode = c.ZipCode,
                BUildingID = c.ID
            }).ToList();
            var mydata = Json(clientList);
            return new JsonResult { Data = mydata, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
        public JsonResult AddClient(Clients model)
        {
            try 
            {

                if (ModelState.IsValid)
                {
                    db.Clients.Add(model);
                    db.SaveChanges();
                }

                var clientList = db.Clients.Select(c => new
                {
                    ClientName = c.ClientName,
                    ClientPhone = c.Phone,
                    ClientFullAddress = c.Address + " " + c.City + " " + c.State + " " + c.ZipCode,
                    ClientEmail = c.Email,
                    ClientFax = c.Fax,
                    ClientAddress = c.Address,
                    ClientCity = c.City,
                    ClientState = c.State,
                    ClientZipcode = c.ZipCode,
                    BUildingID = c.ID
                }).ToList();
                var mydata = Json(clientList);
                return new JsonResult { Data = mydata, JsonRequestBehavior = JsonRequestBehavior.AllowGet };


              
         } 
            catch(Exception e)
            {
                ViewBag.ErrorMessage = e.Message;
                return null;
            }

          
        }
        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);

        }