weixin_33695082 2016-01-28 15:30 采纳率: 0%
浏览 45

AJAX通话为空

I have an AJAX call in the View, and its having difficulties pulling from DB to the populate the drop down list. The DB has been populated. Inside the browser console I can see it has found the correct ID, but I receive the following error:

"Failed to load resource: the server responded with a status of 500 (Internal Server Error)"

Clicking this link take me to a server error:

"The parameters dictionary contains a null entry for parameter 'serviceId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.JsonResult GetServiceDetails(Int32)' in 'YardLad.Controllers.ServiceController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters"

Here is the code I have so far:

AJAX Call:

function GetServices() {
        $.ajax({
            type: "GET",
            url: "/Service/GetServices",
            data: { serviceAreaId: $('#ServiceAreaId').val() },
            datatype: 'JSONP',
            async: true
        })
        .done(function (services) {
            $('#ServiceId').empty(); //commenting this line gets the field to never populate with nulls, leaving the 
                                     //"loading services..." there permanently, also brings up the request a contractor button.
            $.each(services, function (i, service) { //the i is the index being passed to it... service is the value
                $("#ServiceId").append(
                    $('<option/>')
                    .attr('value', this.ServiceId)
                    .text(this.Name)
                );
            });

            GetServiceDescription();
        });
    }

Controller:

//
    // GET: Service/GetServices/

    public JsonResult GetServices(int serviceAreaId)
    {
        // selected service area
        var selectedServiceArea = db.ServiceAreas.Where(sa => sa.ServiceAreaId == serviceAreaId).SingleOrDefault();
        var currentDate = DateTime.Now.AddHours(2);

        // list of contractors that are listed (and not expired) and have at least one contractor service added
        var contractors = db.Contractors.Where(c => c.IsActive == true).Where(c => c.ExpiresOn.Value >= currentDate).Where(c => c.ContractorServices.Count >= 1).ToList();
        HashSet<int> 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);
            }
        }

        // only return services in the selected service area that contractors have added
        HashSet<int> filteredContractorIds = new HashSet<int>(primary.Select(x => x.ContractorId));

        var services = db.ContractorServices.ToList().FindAll(x => filteredContractorIds.Contains(x.ContractorId))
            .Select(x => x.Service).Distinct().OrderBy(s => s.ServiceCategory.Name).ThenBy(s => s.Name)
            .Select(x => new {
                ServiceId = x.ServiceId,
                Name = x.Name
            });

        return this.Json(services, JsonRequestBehavior.AllowGet);
    }

    //
    // GET: Service/GetServiceDetails/1

    public JsonResult GetServiceDetails(int serviceId)
    {
        var details = db.Services.Where(s => s.ServiceId == serviceId)
            .Select(x => new
            {
                Description = x.Description,
                BasePrice = x.BasePrice
            });

        return this.Json(details, JsonRequestBehavior.AllowGet);
    }

.cshtml

@using (Html.BeginForm("RequestService", "Service", FormMethod.Post))
{
<div class="two-third" style="margin-right: 0;">
    <div id="requestService" style="overflow: auto">
        <h2>Request a Service</h2>
        @Html.ValidationSummary(true)

        <div class="display-label">Select a State</div>
        <div class="editor-field">
            <select id="StateId" name="StateId">
                <option value="0">loading states...</option>
            </select>
        </div>

        <div class="display-label">Select a Service Area</div>
        <div class="editor-field">
            <select id="ServiceAreaId" name="ServiceAreaId">
                <option value="0">loading service areas...</option>
            </select>
        </div>

        <div class="display-label">Select a Service</div>
        <div class="editor-field">
            <select id="ServiceId" name="ServiceId">
                <option value="0">loading services...</option>
            </select>
        </div>

        <div class="selectedService" style="display: none;">
            <div class="display-label">Service Description</div>
            <div class="editor-field" id="ServiceDescription"></div>
            <div class="serviceOptions" id="ServiceOptions"></div>
            <div style="text-align: right;">
                <input type="submit" id="SubmitRequest" value="Select a Contractor" />
            </div>
        </div>
    </div>
</div>

<div class="one-third" style="float: right; margin-top: 0;">
    <div id="summary">
        <h3>Yard Lad Service Estimate</h3>
        <p>This is an initial Yard Lad estimate. The end total may be higher or lower based upon the contractor you choose, 
        as well as the contractors on-site assessment. Payment adjustments are easy to make on site or online after the 
        service has been completed.</p>
        <div style="font-weight: bold;">Subtotal: $<span class="subtotal">0.00</span></div>
    </div>
</div>

<input type="hidden" class="stateId" name="StateId" value="0" />
<input type="hidden" class="serviceAreaId" name="ServiceAreaId" value="0" />
<input type="hidden" class="serviceId" name="ServiceId" value="0" />

After changing the Ajax call "url" The server error has slightly changed. I provided a picture of the update below: Server Error Image

  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 GEO下载数据的处理报错 :函数‘Meta’标签‘"data.frame"’找不到继承方法,如何解决?
      • ¥15 DLNM模型是否可以用二分类变量作为y变量
      • ¥15 android object box 一个实体多个表怎么写
      • ¥15 temux 启用docker 服务失败
      • ¥15 Flask 使用celery发送邮件出现‘目标计算机积极拒绝‘
      • ¥60 老人用的sd卡在手机里面不知道操作了什么,导致图片和视频变成了文件,取下sd卡连接电脑就是图中的样子,后缀改为.jpg才可以,需要用系统的画图软件才能打开,文件属性还是文件,有没有批量操作的解决办法
      • ¥15 超时跳出方法代码的返回值问题
      • ¥15 汇编语言程序设计设计,ascii码求数,再求数的BCD码
      • ¥30 Mask rcnn训练自己的数据集出现问题!
      • ¥20 研究人工智能时的几个问题