public ActionResult EditEnrollmentSite(int id)
        {
            var model         = new EnrollmentSiteModel();
            var context       = DataContext.getUasDbContext();
            var jurisdictions = from g in context.uas_Group
                                where g.GroupTypeID == 195
                                select g;

            model.GroupId = id;
            model.Units   = (from j in jurisdictions
                             select new SelectListItem()
            {
                Text = j.GroupName,
                Value = j.GroupID.ToString()
            }).ToList();
            var group = context.uas_Group.SingleOrDefault(x => x.GroupID == id);

            model.Unit           = group.ParentGroupId.ToString();
            model.EnrollmentSite = group.GroupDescription;
            model.SiteNumber     = group.GroupName;
            var address = context.uas_EntGrpAddress.FirstOrDefault(x => x.GroupID == id);

            if (address != null)
            {
                model.Address1 = address.Address1;
                model.Address2 = address.Address2;
                model.City     = address.City;
                model.State    = address.StateProvince;
                model.ZipCode  = address.PostalCode;
                if (!string.IsNullOrWhiteSpace(address.URL))
                {
                    if (address.URL.Contains(";"))
                    {
                        var dates = address.URL.Split(';');
                        model.StartDate = DateTime.Parse(dates[0]);
                        model.EndDate   = DateTime.Parse(dates[1]);
                    }
                }
            }
            model.Restrictions = group.Notes;
            var phone = context.uas_EntGrpPhone.FirstOrDefault(x => x.GroupID == id);

            if (phone != null)
            {
                model.ContactPhone  = phone.PhoneNumber;
                model.ContactPerson = phone.CityAreaCode;
            }

            var email = context.uas_EntGrpEmail.FirstOrDefault(x => x.GroupID == id);

            if (email != null)
            {
                model.ContactEmail = email.EmailAddress;
            }

            return(View(model));
        }
        private static void LoadJurisdictions(EnrollmentSiteModel model, UASEntities context)
        {
            var jurisdictions = from g in context.uas_Group
                                where g.GroupTypeID == 195
                                select g;

            model.Units = (from j in jurisdictions
                           select new SelectListItem()
            {
                Text = j.GroupName,
                Value = j.GroupID.ToString()
            }).ToList();
        }
        public ActionResult EditEnrollmentSite(EnrollmentSiteModel model)
        {
            var context = DataContext.getUasDbContext();

            if (ModelState.IsValid)
            {
                // save the enrollment site
                try
                {
                    var group = context.uas_Group.SingleOrDefault(x => x.GroupID == model.GroupId);
                    group.GroupName        = model.SiteNumber;
                    group.GroupDescription = model.EnrollmentSite;
                    group.Notes            = model.Restrictions;
                    group.ParentGroupId    = int.Parse(model.Unit);
                    var address = context.uas_EntGrpAddress.FirstOrDefault(x => x.GroupID == model.GroupId);
                    if (address == null)
                    {
                        address              = new uas_EntGrpAddress();
                        address.GroupID      = model.GroupId;
                        address.EnterpriseID = group.EnterpriseID;
                        address.CreatedBy    = SessionHelper.LoginStatus.UserID;
                        address.CreatedDate  = DateTime.Now;
                        address.AddressType  = "Site";
                        address.StatusFlag   = "A";
                        // add the new address
                        context.uas_EntGrpAddress.Add(address);
                    }
                    else
                    {
                        address.ModifiedBy   = SessionHelper.LoginStatus.UserID;
                        address.ModifiedDate = DateTime.Now;
                    }
                    address.Address1      = model.Address1;
                    address.Address2      = model.Address2;
                    address.City          = model.City;
                    address.StateProvince = model.State;
                    address.PostalCode    = model.ZipCode;

                    if (model.StartDate.HasValue && model.EndDate.HasValue)
                    {
                        string dates = string.Format("{0:yyyy-MM-dd}", model.StartDate) + ";" +
                                       string.Format("{0:yyyy-MM-dd}", model.EndDate);
                        address.URL = dates;
                    }

                    var phone = context.uas_EntGrpPhone.FirstOrDefault(x => x.GroupID == model.GroupId);
                    if (phone == null)
                    {
                        phone              = new uas_EntGrpPhone();
                        phone.GroupID      = model.GroupId;
                        phone.EnterpriseID = group.EnterpriseID;
                        phone.PhoneType    = "Site";
                        phone.CreatedBy    = SessionHelper.LoginStatus.UserID;
                        phone.CreatedDate  = DateTime.Now;
                        phone.StatusFlag   = "A";
                        context.uas_EntGrpPhone.Add(phone);
                    }
                    else
                    {
                        phone.ModifiedBy   = SessionHelper.LoginStatus.UserID;
                        phone.ModifiedDate = DateTime.Now;
                    }
                    phone.PhoneNumber  = model.ContactPhone ?? " ";
                    phone.CityAreaCode = model.ContactPerson;



                    var email = context.uas_EntGrpEmail.FirstOrDefault(x => x.GroupID == model.GroupId);
                    if (email == null)
                    {
                        email              = new uas_EntGrpEmail();
                        email.GroupID      = group.GroupID;
                        email.EnterpriseID = group.EnterpriseID;
                        email.CreatedBy    = SessionHelper.LoginStatus.UserID;
                        email.CreatedDate  = DateTime.Now;
                        email.StatusFlag   = "A";
                        context.uas_EntGrpEmail.Add(email);
                    }
                    else
                    {
                        email.ModifiedBy   = SessionHelper.LoginStatus.UserID;
                        email.ModifiedDate = DateTime.Now;
                    }
                    email.EmailAddress = model.ContactEmail;

                    context.SaveChanges();
                    return(RedirectToAction("EnrollmentSite"));
                }
                catch (Exception ex)
                {
                    if (ex.IsCritical())
                    {
                        throw ex;
                    }
                    mLogger.Error(ex, "Caught exception saving enrollment site.");
                }
            }
            LoadJurisdictions(model, context);
            return(View(model));
        }