Ravi Bhushan
Ravi Bhushan

Reputation: 115

How to get a <list> from mvc controller to view using jquery ajax

I need to get a list from mvc controller to view using jquery ajax. But I get [object Object].

Ajax code

        $.ajax({
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            url: '/Home/getList',                        
            success: function (data) {
                debugger;
                $scope.StuList = data;
                alert(data);                                
            }
        });

In Controller

    public JsonResult getList()
    {
       Models.clsSave obj = new Models.clsSave();    
       var list = new List<Model>();
       list= obj.getList();
       return Json(list,JsonRequestBehavior.AllowGet);
    }

Upvotes: 1

Views: 4889

Answers (3)

Mox Shah
Mox Shah

Reputation: 3015

I need to get a list from mvc controller to view using jquery ajax. But I get [object Object].

that means you're getting correct data list

how will I store it in $scope.StuList

You can just assign that data to $scope.StuList.

$scope.StuList = data;

Just to verify your data, put it in iteration

$.each($scope.StuList,function(i,v){
  alert(v.sname);
});

You should get the correct data.

Or you can also put it in console, and verify yourself.

console.log($scope.StuList)

Hope this will help, or let me know if you still facing any issue.

Upvotes: 0

Libin C Jacob
Libin C Jacob

Reputation: 1138

If you are getting [Object,Object] means it contains the data. You just Iterate it on the success function of your $.ajax as shown below

success: function (data) {
$.each(data.items, function(item) {
            alert('id :'item.id +' Name:'+item.sname);
            });
}

All the best

Upvotes: 1

Vicky Gonsalves
Vicky Gonsalves

Reputation: 11707

You are getting an Object from ajax response to view that in alert try to stingify your object: alert(JSON.stringify(data))

Upvotes: 0

Related Questions