0

I need to read each value from a json result i request to a controller action. this is my client side.

$.ajax({
        type: "POST",
        url: "List",
        data: { firstArray: arrayCountries, secondArray: arrayLang },
        success: function (result) {

             //here i need each value from json, but it's not working for me   
        },
        dataType: "json",
        traditional: true
    });

Ok now i use this way because i need to send 2 javascript arrays and after finding a lot this way works for me.

And this is my server side. According to How can I post an array of string to ASP.NET MVC Controller without a form? i have created this.

public ActionResult List(List<String> firstArray, List<String> secondArray)
{
    //But here i need a list of sections, so i call a method for that
      var sections = SomeClass.getSections(fistArray, secondArray);

    //And return the json with the collection 
      return Json(sections, JsonRequestBehavior.AllowGet);
}

Now this is my getSections Method

public static IEnumerable getSections(List<String> firstArray, List<String>secondArray)
{
    entities db = new entities();

    var result = from values in db.Sections
                 where ... 
                 select new SectionsModel{ // do something  };

    return result.OrderBy(p => p.Description);
}

Now im getting well json in client side but i don't know how to acces it, and how can i get each value from it ? ... thanks in advance.

1 Answer 1

0

You need to parse it first, after that you can just access the properties similarly as what you would do in .NET

$.ajax({
        type: "POST",
        url: "List",
        data: { firstArray: arrayCountries, secondArray: arrayLang },
        success: function (result) {

             var sections = JSON.parse(result);

             // access properties here
        },
        dataType: "json",
        traditional: true
    });
Sign up to request clarification or add additional context in comments.

4 Comments

im getting firebug JSON.parse: unexpected character ... is something wrong with json format ?
What is the value of result when the call completes?
it's something like [object Object],[object Object]
thanks for your help finally with result[0].property i can get values. :)

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.