Reputation: 4116
I am using following Javascript function to populate the 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";
}
}
});
}
Following is the method inside my controller:
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);
}
When I am trying to run this, it gets returned from Controller. But, after that, I get error in my VS:
JavaScript runtime error: 'response' is undefined
I think, something is missing in the function GetDropDownData()
, but not able to figure out what that exactly is.
Will you please guide me? Thanks!
Upvotes: 0
Views: 123
Reputation: 2053
In your AJAX request you need to change it to:
success: function ( response ) {
$.map(response, function (item) {
$('#custServiceContactsSelection').append($("<option />").val(item.ContactName).text(item.ContactName));
});
},
// rest of code
Upvotes: 1