[英]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. 我需要从mvc控制器获取列表以使用jquery ajax进行查看。 But I get
[object Object]
. 但是我得到
[object Object]
。
Ajax code Ajax代码
$.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);
}
您正在从ajax响应中获取一个对象,以查看警报中如何尝试实例化您的对象: alert(JSON.stringify(data))
If you are getting [Object,Object] means it contains the data. 如果获取[Object,Object],则表示它包含数据。 You just Iterate it on the success function of your $.ajax as shown below
您只需对$ .ajax的成功函数进行迭代,如下所示
success: function (data) {
$.each(data.items, function(item) {
alert('id :'item.id +' Name:'+item.sname);
});
}
All the best 祝一切顺利
I need to get a list from mvc controller to view using jquery ajax.
我需要从mvc控制器获取列表以使用jquery ajax进行查看。 But I get [object Object].
但是我得到了[object Object]。
that means you're getting correct data list 这意味着您将获得正确的数据列表
how will I store it in $scope.StuList
我如何将其存储在$ scope.StuList中
You can just assign that data
to $scope.StuList
. 您可以将该
data
分配给$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. 希望这会有所帮助,或者让我知道您是否仍然遇到任何问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.