繁体   English   中英

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

[英]Populating data in Datalist issue

我使用下面的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM