MVC3,AJAX如何获取和遍历集合啊,已经第4次问了...哪位大神帮帮忙
这个是我的代码
$.ajax({
type: 'Post',
url: '@Url.Action("GetJSON", "Article")',
dataType: "json",
success: function (data) {
alert(data.length);---长度总是为1
$.each(data, function (i, item) {
alert(item.CatalogId);
});
}
});
上面有问题么?
还是这里的问题
[HttpPost]
public JsonResult GetJSON()
{
return Json(new Catalog().GetPathItems());
}
public class Catalog{
//属性省略
public List<Catalog> GetPathItems(){
List<Catalog> list=new List<Catelog>();
Catelog c1=new Catalog{name="a",CatalogId=2,....};
list.Add(c1);
.
.
.
Catelog c30=new Catalog{name="a",CatalogId=2,....};
list.Add(c30);
return list;
}
}
怎么样才能把添加进去的对象,全部打印出来?
------解决方案--------------------
Control方法
C# code
[HttpPost]
public JsonResult GetJSON()
{
return Json(new Catalog().GetPathItems());
}
public class Catalog
{
//属性省略
public int CatalogId { get; set; }
public string name { get; set; }
public List<Catalog> GetPathItems()
{
List<Catalog> list = new List<Catalog>();
Catalog c1 = new Catalog { name = "a", CatalogId = 2 };
list.Add(c1);
Catalog c2 = new Catalog { name = "a", CatalogId = 2 };
list.Add(c2);
return list;
}
}
------解决方案--------------------
LZ是不是返回的json格式不对啊