填充数据列表问题中的数据

Populating data in Datalist issue

本文关键字:数据 列表 填充 问题      更新时间:2023-09-26

我正在使用以下Javascript函数来填充Datalist:

function GetDropDownData(f) {
    $.ajax({
        url: '/Rentals/Base/GetContactsForFacility?selectedFacility=' + f,
        data: { facility: f },
        dataType: 'json',
        success: function (result) {
            response($.map(result, function (item) {
                $('#custServiceContactsSelection').append($("<option     />").val(item.ContactName).text(item.ContactName));
            }));
        },
        cache: false,
        error: function (jqXHR, textStatus, errorThrown) {
            if (errorThrown.indexOf("Your session has timed out") != -1) {
                location.href = "/Rentals/Base/Timeout";
            }
        }
    });
}

以下是我的控制器内部的方法:

    public ActionResult GetContactsForFacility (string selectedFacility)
    {
        var facilityId = new Guid(selectedFacility);
        if (Request.IsAjaxRequest())
        {
            var contacts = SessionService.AllCustomerServiceContactsForFacility(CrmService, facilityId);

            return Json(contacts.Select(x => new {  label = x.ContactName }), JsonRequestBehavior.AllowGet); 
        }
        return Content(string.Empty);
    }

当我尝试运行这个时,它会从Controller返回。但是,在那之后,我在VS中出错:JavaScript runtime error: 'response' is undefined

我认为,函数GetDropDownData()中缺少了一些东西,但无法弄清楚到底是什么

请你给我带路好吗?谢谢

在AJAX请求中,您需要将其更改为:

success: function ( response ) {
            $.map(response, function (item) {
                $('#custServiceContactsSelection').append($("<option     />").val(item.ContactName).text(item.ContactName));
            });
        },
        // rest of code