Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am sending List <trip> class(id,tripdetails,date) from my controller to my Ajax,
i am unable to decode the list on Success function of Ajax, How i can get all value of my List<trip> in Ajax success function.

my code is like this
controller:
C#
[HttpGet]
public ActionResult TripLoad()
{
    List<trip> lst = repo.TripDetails();
    return Json(lst, JsonRequestBehavior.AllowGet);  
}

JavaScript
//ajax
$.ajax({

        url: '/Trip/TripLoad',

        //url: @Url.Action("TripLoad", "Trip"),
        dataType: "json",
        type: "GET",
        contentType: 'application/json; charset=utf-8',
        cache: false,
        data: {},
        success: function (data) {   //I want here All List of Trip data or how i can use loop to get data here. 

            alert("Received " + data)
           var d=JSON.stringify(data);
           // alert(d.GetAllResult.Name);
           
        },
        error: function (xhr) {
            alert("Error Message is sanjib " + xhr.responseText);
        }
});
Posted
Updated 25-Oct-15 20:35pm
v2
Comments
Amit Jadli 26-Oct-15 2:42am    
Any error you are getting???

Do something like this,
JavaScript
success: function(data)
{
    for(var i=0; i <data.length; i++)    {
        console.log(data[i].TripID);   // I don't know your model structure, so I assumed one property...
    }
}

Data is already received as a JSON, you don't have to convert it again to it.

Now, iterate through list using for loop and use your model property to get the value.
If you have any query, we'll discuss it further. :)


-KR
 
Share this answer
 
v2
Comments
Sanjit Rajbanshi 26-Oct-15 9:26am    
thank you very much..
It's quite unclear why would you ever stringify data received from the server. Even if the data is not JSON, what would you do with JSON representation of it? I would rather assume that the server gives you JSON form, and you need to do the opposite to stringify, which is, not surprisingly JSON.parse:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse[^],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900