public JsonResult _UpdateSubContractorsF(CancerTypeViewModel cancertypeviewmodel)
        {
            if (ModelState.IsValid)
            {
                // Get List of SubContractors
                var cliniclist1 = (from y in db.SubContractorClinics
                                   where y.SiteID == cancertypeviewmodel.SiteID
                                   select y).ToList();


                foreach (SubContractorClinics cas in cliniclist1)
                {
                    // Remove subcontractors
                    var subcontractorlist = (from x in db.SubContractorClinics
                                             where x.SiteID == cas.SiteID && x.ProgramID == cancertypeviewmodel.ProgramID && x.CancerTypeBinID == cancertypeviewmodel.CancerTypeBinID && x.IsClinic == false
                                             //where x.ProgramBinID == agencyroleviewmodel.ProgramID && x.AgencyRoleBinID == agencyroleviewmodel.AgencyRoleBinID
                                             select x).ToList();


                    foreach (SubContractorClinics acc in subcontractorlist)
                    {
                        db.SubContractorClinics.Remove(acc);
                        db.SaveChanges();
                    }
                }

                if (cancertypeviewmodel.FacilityList != null)
                {
                    // Facilities
                    var stringToSplit = cancertypeviewmodel.FacilityList;

                    var query = from val in stringToSplit.Split(',')
                                select Convert.ToInt32(val);

                    foreach (int val in query)
                    {
                        // Loop through subcontractors
                        SubContractorClinics scc = new SubContractorClinics
                        {
                            SubContractorID = val,
                            SiteID          = cancertypeviewmodel.SiteID,
                            ClinicID        = Convert.ToInt32(cancertypeviewmodel.SiteID),
                            ProgramID       = Convert.ToInt32(cancertypeviewmodel.ProgramID),
                            CancerTypeBinID = Convert.ToInt32(cancertypeviewmodel.CancerTypeBinID),
                            IsClinic        = false
                        };

                        db.SubContractorClinics.Add(scc);
                        db.SaveChanges();
                    }
                }


                return(Json(new { Status = "Success", Modified = cancertypeviewmodel.SiteID }, JsonRequestBehavior.AllowGet));
            }


            return(Json(new { Status = "Success", Modified = cancertypeviewmodel.SiteID }, JsonRequestBehavior.AllowGet));
        }
        public JsonResult _AddSubContractorF(SubContractorViewModel subcontractorviewmodel)
        {
            if (ModelState.IsValid)
            {
                // Set Record Info
                string   UserNameInit = @User.Identity.Name.ToString();
                DateTime CreatedInit  = DateTime.Now;

                var userid = (from x in db.AspNetUsers
                              where x.UserName == UserNameInit
                              select x).FirstOrDefault();



                if (subcontractorviewmodel.SubContractorID != null)
                {
                    // Update SubContractor


                    SubContractor subcontractor = db.SubContractors.Single(x => x.SubContractorID == subcontractorviewmodel.SubContractorID);
                    subcontractor.SubContractorName = subcontractorviewmodel.SubContractorName;
                    subcontractor.Active            = subcontractorviewmodel.Active;
                    subcontractor.Address           = subcontractorviewmodel.Address;
                    subcontractor.CityBinID         = subcontractorviewmodel.CityBinID;
                    subcontractor.StateBinID        = subcontractorviewmodel.StateBinID;
                    subcontractor.ZipCode           = subcontractorviewmodel.ZipCode;
                    subcontractor.CountyBinID       = subcontractorviewmodel.CountyBinID;
                    subcontractor.DateUpdated       = CreatedInit;
                    subcontractor.UpdatedBy         = UserNameInit;
                    db.SaveChanges();


                    return(Json(new { Status = "Success", Modified = subcontractorviewmodel.SubContractorID }, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    // Create SubContractor

                    SubContractor sc = new SubContractor
                    {
                        SubContractorName = subcontractorviewmodel.SubContractorName,
                        Active            = subcontractorviewmodel.Active,
                        Address           = subcontractorviewmodel.Address,
                        CityBinID         = subcontractorviewmodel.CityBinID,
                        StateBinID        = subcontractorviewmodel.StateBinID,
                        ZipCode           = subcontractorviewmodel.ZipCode,
                        CountyBinID       = subcontractorviewmodel.CountyBinID,
                        DateUpdated       = CreatedInit,
                        UpdatedBy         = UserNameInit
                    };


                    try
                    {
                        db.SubContractors.Add(sc);
                        db.SaveChanges();
                    }
                    catch (DbEntityValidationException dbEx)
                    {
                        foreach (var validationErrors in dbEx.EntityValidationErrors)
                        {
                            foreach (var validationError in validationErrors.ValidationErrors)
                            {
                                Debug.WriteLine("Property: {0} Error: {1}",
                                                validationError.PropertyName, validationError.ErrorMessage);
                            }
                        }
                    }

                    // SubContractor ID
                    var SCID = sc.SubContractorID;


                    if (subcontractorviewmodel.CancerTypeName != null)
                    {
                        // Cancer Types

                        var stringToSplitA = subcontractorviewmodel.CancerTypeName;

                        var queryA = from valA in stringToSplitA.Split(',')
                                     select Convert.ToInt32(valA);

                        foreach (int valA in queryA)
                        {
                            // Add to AgencyContactClinics

                            SubContractorClinics subcontractorclinics = new SubContractorClinics
                            {
                                SubContractorID = SCID,
                                SiteID          = subcontractorviewmodel.SiteID,
                                ClinicID        = Convert.ToInt32(subcontractorviewmodel.SiteID),
                                ProgramID       = Convert.ToInt32(subcontractorviewmodel.ProgramID),
                                CancerTypeBinID = valA,
                                IsClinic        = false
                            };

                            db.SubContractorClinics.Add(subcontractorclinics);
                            db.SaveChanges();
                        }
                    }



                    return(Json(new { Status = "Success", Modified = subcontractorviewmodel.SubContractorID }, JsonRequestBehavior.AllowGet));
                }
            }


            return(Json(new { Status = "Success" }, JsonRequestBehavior.AllowGet));
        }