how to get data from json?

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":"футбол"}]

Answers


From the JSON output you've written out, I'd rewrite it a little bit like this:

$.ajax({
    url: urlString,
    type: 'POST',
    data: JSON.stringify({ id: districtId }),
    dataType: 'json',
    contentType: 'application/json',
    cache: 'false',
    success: function (data) {
        if (!data) return; // if data is empty, return

        var $select = $('select#ChangeOnsubCategoryId');
          // put the select element in a variable so you don't need to
          // jquery select it again

        $select.append('<option value="0">Select One</option>');
          // I guess you first want to add a "select one" option

        // data should be an array e.g. [{"Id":"53","Name":"футбол"}]
        $.each(data, function (key, manager) {
           // for each "manager" in the "data" array

           // manager should be an object in the data
           // array e.g. {"Id":"53","Name":"футбол"}
           // no need to for each it

           $select.append(
                    '<option value="' + manager.Id + '">'
                    + manager.Name +
                    '</option>');
             // add the manager in the select box
        });
    }
});

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).


$.each(MyDataNameAndId, function (index, manager) {
                $('select#ChangeOnsubCategoryId').append(
                        '<option value="' + manager.Id + '">'
                        + manager.Name +
                        '</option>');
            });

should be just

$('select#ChangeOnsubCategoryId').append(
         '<option value="' + MyDataNameAndId.Id + '">'
         + MyDataNameAndId.Name +
         '</option>');

The sample code works for me without exceptions which leads me to believe there's something wrong with the JSON being returned. Check that it actually returns the string you posted.


Need Your Help

How to deploy multi - tier application with Kubernetes

kubernetes

If I have a multi - tier application (say web / logic / database), where each tier having it's own container, and I need to deploy all of these en - bloc, do they all have to go into the same pod?

Consuming xml using HttpClient from webapi

.net asp.net-web-api dotnet-httpclient

I used WebClient to get an Xml object from a restfull service (.net web api) and everything worked great: