What I missing about posting to ActionMethods to pull data here ?
I need to do a simple javascript/jquery call to an ActionMethod that will populatel my drop down menu with data from my Json object of type IQueryable ( or List ).
So I tried two options:
function populateuUsers(id) {
$.post("/User/GetUsersJson/", { id: id }, function (data) {
if ($.isEmptyObject(data)) {
alert("Empty");
}
$.each(data, function (item) {
alert(item);
$("#user-list").append(
$("<option></option>").text(data[item].FullName).val(data[item].Id)
);
});
}, "json");
}
I get an error that “data is undefined”
Then I tried:
function populateUsers(id) {
$.ajax({
type: 'post',
datatype: 'json',
url: "/User/GetUsersJson/" + id,
success: function (data) {
alert(data);
$.each(data, function (item) {
alert(item);
$("#users-list").append(
$("<option></option>").text(data[item].FullName).val(data[item].Id));
});
},
error:function()
{
alert(data);
}
});
}
Doesn’t work either.
Something to note…I set break points and did verify that data in GetUsersJson Action method User controller gets properly populated and returned to the javascript function.
Why is it not working then ? Why is “data undefined” and it acts like there nothing returned ?
Thank you in advance.
Inside the anonymous function that you’re passing to
$.each,datais not defined. Instead you should pass two variables to that function. Something like:http://api.jquery.com/jQuery.each/