6

I have a controller that returns a list of custom linq-to-sql model objects in JSON format to jquery ajax calls:

List<MyAppLibrary.Model.Search> listSearches = search.ToList();
        return new JsonResult { Data = listSearches };

I have the following javascript which gets the response:

$.getJSON("/ajax/getbrands",
    function(data) {
        alert(data);
    });

I'd like to know how I can process that data response in javascript? How do I get the Name paramter of the Model.Search object?

Thanks.

1
  • If you can show the json returned by method it will be of immense help! Commented Jul 31, 2009 at 3:02

3 Answers 3

7

The data variable that gets returned from the jQuery AJAX call contains the JSON object. You can access the fields of each of your MyAppLibrary.Model.Search objects in your JavaScript like so:

// this will grab the Search object at index 0 of your list
// and put the Name property's value of the Search object
// into a var
var firstItemName = data.Data[0].Name;
Sign up to request clarification or add additional context in comments.

Comments

6

The data argument will have a Data property, which is your list of Search models.

 $.getJSON("/ajax/getbrands",
        function(data) {
             $.each(data.Data, function(i, item) {
                  // ... item will be a Search model...
                  // ... i will be the index of the item in the list...
                  // ...
             });
        }
 );

1 Comment

I used this format, but had to change the each line to $.each(data, function(i,item){ for it to work. Interestingly the jquery site says to use $.each(data.items, function(i,item){ but that didn't work for me with jquery.1.3.2
1

You can use jquery.json plugin play with the JSON returned.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.