控制器中的空引用

时间:2016-02-16 18:48:37

标签: c# mysql ajax asp.net-mvc

我有一个MVC项目。在这个项目中,一个人可以从下拉列表中选择草坪护理服务。

当我点击"选择承包商"按钮我收到服务器错误"对象引用未设置为对象的实例。"。

它指向c:\ Users \ info_000 \ Source \ Repos \ yardladqa中的" YardLad.Controllers.ServiceController.CheckForAvailableContractors(Int32 serviceId,String [] serviceOptionIds,String [] serviceOptionItemIds,Int32 serviceAreaId) \控制器\ ServiceController.cs:990"

控制器:

// GET: Service/CheckForAvailableContractors/

    public JsonResult CheckForAvailableContractors(int serviceId, string[] serviceOptionIds, string[] serviceOptionItemIds, int serviceAreaId)
    {
        var contractorsAvailable = false;

        // list of contractors that are listed and have at least one contractor service added
        var contractors = db.Contractors.Where(c => c.IsActive == true).Where(c => c.ContractorServices.Count >= 1).ToList();
        HashSet<int> contractorIds = new HashSet<int>(contractors.Select(x => x.ContractorId));

        // all the contractor services that match the service id, filtered by active/listed contractors
        var contractorServices = db.ContractorServices.ToList().FindAll(x => contractorIds.Contains(x.ContractorId))
            .Where(cs => cs.ServiceId == serviceId).Where(cs => cs.IsActive == true);

        // filter the previous collection of contractors to include only the contractors that have the service selected
        contractors = contractorServices.Select(x => x.Contractor).ToList();
        contractorIds = new HashSet<int>(contractors.Select(x => x.ContractorId));

        // filter list of contractors to include only those who offer services in the selected service area
        var primary = db.Contractors.ToList().FindAll(x => contractorIds.Contains(x.ContractorId))
            .Where(c => c.ServiceAreaId == serviceAreaId).Distinct().ToList();

        var secondary = db.ContractorServiceAreas.ToList().FindAll(x => contractorIds.Contains(x.ContractorId))
            .Where(csa => csa.ServiceAreaId == serviceAreaId).Select(x => x.Contractor).Distinct().ToList();

        foreach (var contractor in secondary)
        {
            if (primary.Contains(contractor) == false)
            {
                primary.Add(contractor);
            }
        }

        HashSet<int> filteredContractorIds = new HashSet<int>(primary.Select(x => x.ContractorId));
        contractors = contractors.FindAll(x => filteredContractorIds.Contains(x.ContractorId));
        contractorIds = new HashSet<int>(contractors.Select(x => x.ContractorId));

        // gather selected service, options, and option items to filter by
        var service = db.Services.Where(s => s.ServiceId == serviceId).Where(s => s.IsActive == true).SingleOrDefault(); // selected service
        List<ServiceOption> serviceOptions = new List<ServiceOption>(); // selected service options
        List<ServiceOptionItem> serviceOptionItems = new List<ServiceOptionItem>(); // selected service option items

        // get the service options (populate list)
        foreach (var serviceOptionId in serviceOptionIds)
        {
            int id = int.Parse(serviceOptionId);
            serviceOptions.Add(db.ServiceOptions.Where(so => so.ServiceOptionId == id)
                .Where(so => so.IsActive).SingleOrDefault());
        }

        // extract the names
        HashSet<string> serviceOptionNames = new HashSet<string>(serviceOptions.Select(x => x.Name));

        // get the service option items (populate list)
        foreach (var serviceOptionItemId in serviceOptionItemIds)
        {
            int id = int.Parse(serviceOptionItemId);
            serviceOptionItems.Add(db.ServiceOptionItems.Where(soi => soi.ServiceOptionItemId == id)
                .Where(soi => soi.IsActive == true).SingleOrDefault());
        }

        // extract the names
        HashSet<string> serviceOptionItemNames = new HashSet<string>(serviceOptionItems.Select(x => x.Name));

        // get contractor availabilities from the filtered list
        var contractorAvailabilities = db.ContractorAvailabilities.ToList().FindAll(x => contractorIds.Contains(x.ContractorId)).ToList();

        // if there is an option for selecting the date, store what the users selected
        var selectedDayOptionId = serviceOptions.Where(so => so.Name.ToLower() == "scheduled day").SingleOrDefault().ServiceOptionId;
        var selectedDayValue = serviceOptionItems.Where(soi => soi.ServiceOptionId == selectedDayOptionId).SingleOrDefault().Name;

服务器错误指向&#34; var selectedDayOptionId = serviceOption.Where(so =&gt; so.Name.ToLower()==&#34;预定日期&#34;)。SingleOrDefault()。Name;

调试时我注意到了selectedDayValue

我坚持要看什么或者做什么来进一步解决这个问题。我将非常感谢我可以采取哪些措施来缩小范围。

0 个答案:

没有答案