public ActionResult Add()
        {
            //object
            var obj = new Utilities_Datos_Generales();
            //sites list
            Sites s = new Sites();
            s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
            obj.Sites = s;

            return View(obj);
        }
        public ActionResult Update(Utilities_Datos_Generales u)
        {
            String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;

            try
            {

                //getting sites with same name as new site name
                var sameName = from s in db.Utilities_Datos_Generales where s.NombreUtility.Equals(u.NombreUtility) select s;

                if (u != null)
                {
                    if (sameName.Count() > 1)
                    {
                        TempData["previous"] = thisPageUrl;
                        TempData["message"] = "The name you are trying to use is already taken.";
                        return View("~/Views/Shared/Error.cshtml");
                    }
                    else
                    {
                        u.LastUpdatedBy = LoggedUser();
                        u.SiteID = int.Parse(u.SelectedSite);
                        u.LastUpdatedDate = DateTime.Now;
                        db.Entry(u).State = System.Data.Entity.EntityState.Modified;
                        db.SaveChanges();
                        TempData["previous"] = thisPageUrl;
                        TempData["message"] = "The utility was updated.";
                        return View("~/Views/Shared/Success.cshtml");
                    }
                }
                else
                {
                    TempData["previous"] = thisPageUrl;
                    TempData["message"] = "There was an error updating site.  Try again or contact administrator.";
                    return View("~/Views/Shared/Error.cshtml");
                }

            }
            catch (Exception e)
            {
                TempData["previous"] = thisPageUrl;
                TempData["message"] = "There was an error updating site.  Try again or contact administrator.";
                return View("~/Views/Shared/Error.cshtml");
            }
        }
        public ActionResult Add(Utilities_Datos_Generales u)
        {
            String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
            TempData["previous"] = thisPageUrl;

            //getting site ID
            int SiteID = 0;
            if (!string.IsNullOrEmpty(u.SelectedSite))
            {
                try
                {
                    SiteID = int.Parse(u.SelectedSite);
                    var site = db.Sites.Find(SiteID);

                    if (site != null)
                    {
                        if (!string.IsNullOrEmpty(u.AceptaAnulacion))
                        {
                            u.SiteID = SiteID;
                            u.RegisteredBy = LoggedUser();
                            u.RegisteredDate = DateTime.Now;
                            //saving database
                            db.Utilities_Datos_Generales.Add(u);
                            db.SaveChanges();

                            TempData["message"] = "The utilitie was added.";
                            return View("~/Views/Shared/Success.cshtml");
                        }
                        else
                        {
                            ModelState.AddModelError("AceptaAnulacion", "You must select an option.");
                            //object
                            var obj = new Utilities_Datos_Generales();
                            //sites list
                            Sites s = new Sites();
                            s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
                            obj.Sites = s;

                            return View(obj);
                        }
                    }
                    else
                    {
                        TempData["message"] = "The site you selected doesn't exist. Try again with another site.";
                        return View("~/Views/Shared/Error.cshtml");
                    }

                }
                catch (Exception e)
                {
                    TempData["message"] = "There was an error registering utility.  Try again or contact administrator.";
                    return View("~/Views/Shared/Error.cshtml");
                }
            }
            else
            {
                ModelState.AddModelError("Site", "You must select a site.");
                //object
                var obj = new Utilities_Datos_Generales();
                //sites list
                Sites s = new Sites();
                s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
                obj.Sites = s;

                return View(obj);
            }
        }
        public ActionResult Search(string SelectedSite)
        {
            String thisPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
            TempData["previous"] = thisPageUrl;

            Sites site = new Sites();
            if (!string.IsNullOrEmpty(SelectedSite))
            {
                int SiteID = int.Parse(SelectedSite);
                site = db.Sites.Find(SiteID);
                if (site != null)
                {
                    //utilities
                    var utilities = from b in db.Utilities_Datos_Generales where b.SiteID == SiteID select b;
                    Utilities_Datos_Generales utility = new Utilities_Datos_Generales();
                    site.SitesList = new SelectList(site.GetAllSites(), "SiteID", "SiteName");
                    utility.Sites = site;
                    utility.SiteUDG = utilities.ToList();
                    ViewBag.Error = false;
                    return View(utility);
                }
                else
                {
                    ViewBag.Message = "There are none utilities for the selected site.";
                    return View();
                }
            }

            ViewBag.Error = true;
            ModelState.AddModelError("Site", "You must select a site.");
            var obj = new Batch();
            site = new Sites();
            site.SitesList = new SelectList(site.GetAllSites(), "SiteID", "SiteName");
            obj.Sites = site;
            return View(obj);
        }
 //Search by Site
 public ActionResult Search()
 {
     var obj = new Utilities_Datos_Generales();
     var s = new Sites();
     s.SitesList = new SelectList(s.GetAllSites(), "SiteID", "SiteName");
     obj.Sites = s;
     return View(obj);
 }
 //List All Utilities
 public ActionResult AllUtilities()
 {
     Utilities_Datos_Generales u = new Utilities_Datos_Generales();
     var us = u.GetAllUDG();
     if (us.Count() > 0)
     {
         return View(us.ToList());
     }
     else
     {
         ViewBag.Message = "There are not registered utilities.";
         return View();
     }
 }