public ActionResult Create(WebsiteServiceProvider provider)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    this.ResetForInvalidModel(provider);

                    this.SetErrorMessage();

                    return(this.View(provider));
                }

                // In each location, don't create a location contact if no information is given.
                foreach (var curLocation in provider.Locations)
                {
                    curLocation.StateId = int.Parse(curLocation.StateIdString);
                    if (this.IsContactPersonEmpty(curLocation.ContactPerson))
                    {
                        curLocation.ContactPerson = null;
                    }
                }

                var webToDb = new WebToDatabaseServiceProvider();
                provider.State = ObjectStatus.ObjectState.Create;

                // Need an empty service areas object if no service areas were included.
                if (provider.Services == null)
                {
                    provider.Services = new WebServiceAreas();
                }

                webToDb.UpdateServiceProvider(provider, this.UserId);
                this.TempData["Info"] = string.Format("The provider {0} was created succesfully.", provider.Name);
                return(this.RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                this.SetupViewBag();
                this.SetupLocationStateDropdown(provider);
                foreach (var curLocation in provider.Locations.Where(curLocation => curLocation.Coverage == null))
                {
                    curLocation.Coverage = new List <Coverage>();
                }

                this.TempData["Error"] = "An error occured while saving the service provider.";
                return(this.View(provider));
            }
        }
        public ActionResult Delete(int id)
        {
            var provider = this.dataLogics.GetServiceProviderById(id);

            provider.State = ObjectStatus.ObjectState.Delete;
            var webToDb = new WebToDatabaseServiceProvider();

            if (!webToDb.UpdateServiceProvider(provider, this.UserId))
            {
                this.TempData["Error"] = "An error occured while deleting the service provider.";
            }
            else
            {
                this.TempData["Info"] = "The provider was deleted succesfully.";
            }
            return(this.RedirectToAction("Index"));
        }
        public ActionResult Edit(WebsiteServiceProvider provider)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    this.ResetForInvalidModel(provider);
                    this.SetErrorMessage();
                    return(this.View(provider));
                }

                var original = this.dataLogics.GetServiceProviderById(provider.Id);

                if (provider.Services == null)
                {
                    provider.Services = new WebServiceAreas
                    {
                        State        = ObjectStatus.ObjectState.Update,
                        ServiceAreas = new List <int>()
                    };
                }

                // Check against original locations to see which are new, and what has been removed.
                this.SetStateFlagsLocations(provider, original);

                provider.State = ObjectStatus.ObjectState.Update;

                var webToDb = new WebToDatabaseServiceProvider();
                webToDb.UpdateServiceProvider(provider, this.UserId);

                this.TempData["Info"] = string.Format("The provider {0} was updated succesfully.", provider.Name);

                if (User.IsInRole("1"))
                {
                    return(this.RedirectToAction("Index"));
                }

                return(this.RedirectToAction("Details", new { id = provider.Id }));
            }
            catch (Exception ex)
            {
                return(this.View());
            }
        }