我正在使用以下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()
中缺少某些东西,但却无法确定究竟是什么。
答案 0 :(得分:1)
在您的AJAX请求中,您需要将其更改为:
success: function ( response ) {
$.map(response, function (item) {
$('#custServiceContactsSelection').append($("<option />").val(item.ContactName).text(item.ContactName));
});
},
// rest of code