在Asp.Net MVC从控制器返回的显示JSON数组数据 [英] Display JSON Array Data Returned from Controller in Asp.Net MVC
本文介绍了在Asp.Net MVC从控制器返回的显示JSON数组数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个操作方法在我的控制器;
I have an action method in my controller as;
public ActionResult IndexWithJson(int Id, int? page)
{
int pageSize = 2;
int pageNumber = (page ?? 1);
using (var adsRepo = new AdvertisementRepository())
{
if (Id > 0)
{
return Json(new{
Data = adsRepo.GetAdvertisementBySubCategoryId(Id).ToPagedList(pageNumber, pageSize)
}, JsonRequestBehavior.AllowGet);
}
else
{
return View("404");
}
}
}
在浏览器的结果输出;
正如你可以在图片中看到的,我有一个名为数据的对象。
一个包备份数据(在控制器以及)
现在,当我把这个使用jQuery这样的;
the result output in the browser is;
As you can see in the picture, I have an wrap up the data (in controller as well) with an object called "Data".
Now when I call this using jQuery like this;
var serviceBase = "/catalog/";
$.ajax({
url: serviceBase + 'IndexWithJson',
contentType: 'application/json; charset=utf-8',
dataType:'json',
data: { Id: categoryId, page: page },
success: function (data) {
alert(data.Title);
}
});
我看到警告框为空。
这里有什么问题吗?
如何我其实只显示一个属性(见来回属性图片)的警告框。
是我的DataType特性就在jQuery的AJAX功能? (我想这应该是JSONP
推荐答案
试试这个:
data.Data[0].Title
这应该是工作
这篇关于在Asp.Net MVC从控制器返回的显示JSON数组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文