Reputation:
The controller´s method:
[HttpGet]
public ActionResult GetItem()
{
List<SelectListItem> drop = new List<SelectListItem>
{
new SelectListItem{Value="Superman",Text="Superman"},
new SelectListItem{Value="Batman",Text="Batman"},
new SelectListItem{Value="Wonderwoman",Text="Wonderwoman"}
};
return Json(drop);
}
The HTML´s select:
<select id="ddlCustomers"></select>
The AJAX´s call:
var ddlCustomers = $("#ddlCustomers");
ddlCustomers.empty().append('<option selected="selected" value="0" disabled = "disabled">loading.........</option>');
$.ajax({
type: "GET",
url: "/Usuario/GetItem",
dataType: 'JSON',
contentType: "application/json",
success: function (data) {
alert(data);
for (var i = 0; i < data.length; i++) {
$('#ddlCustomers').append('<option value=' + data[i].Value + '>' + data[i].Text + '</option > ');
}
}
});
It´s hitting the controller but returning/populating the select as UNDEFINED.
Any helps ? Thank you!
please note: this is a WEB APP not WEB API.
Upvotes: 1
Views: 1462
Reputation: 53958
Based on the response you get, you should change this:
'<option value=' + data[i].Value + '>' + data[i].Text + '</option > '
with this:
'<option value=' + data[i].value + '>' + data[i].text + '</option > '
By the way you don't have to select HTML element with id ddlCustomer
in each step of your for loop. You can use ddlCustomers
variable who holds a reference to the element you want to append the options you have
for (var i = 0; i < data.length; i++) {
ddlCustomers.append('<option value=' + data[i].value + '>' + data[i].text + '</option > ');
}
Upvotes: 0
Reputation: 134
JS is case sensitive, try
$('#ddlCustomers').append('<option value=' + data[i].value + '>' + data[i].text + '</option > ');
Upvotes: 0