I have controller
var subCategories = m_listsRepository.GetSubCategories(id);
var items = subCategories.Select(x=>new MyDataNameAndId(){Id = x.Value, Name = x.Text});
return Json(items);
And ajax:
$.ajax({
url: urlString,
type: 'POST',
data: JSON.stringify({ id: districtId }),
dataType: 'json',
contentType: 'application/json',
cache: 'false',
success: function (data) {
alert("success");
$.each(data, function (key, MyDataNameAndId) {
alert(key);//== 0
alert(MyDataNameAndId);// then throws
$('select#ChangeOnsubCategoryId').append('<option value="0">Select One</option>');
$.each(MyDataNameAndId, function (index, manager) {
$('select#ChangeOnsubCategoryId').append(
'<option value="' + manager.Id + '">'
+ manager.Name +
'</option>');
});
});
}
});
what am I doing wrong?
UPDATE:
Controller is worked.
alert(“success”); – is show
alert(key); – is show 0
alert(MyDataNameAndId); – not show.
I need generate in ‘select#ChangeOnsubCategoryId’ options from select#ChangeOnsubCategoryId
How do this? this understand?
I do not know how to show what passed json
json string:
[{"Id":"53","Name":"футбол"}]
From the JSON output you’ve written out, I’d rewrite it a little bit like this:
I’m assuming stuff though. Use a javascript debugger (such as developer tools in Chrome or Firebug in Firefox) to check what values you get from the ajax call. Put the breakpoint inside the function and then watch what the data becomes (in this case it should be an array of objects containing Id and Name).