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");
}
}
}
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);
}
});
I see null in alert box.
What is the problem here?
How do I actually display just one property (see the picture fro properties) in alert box.
Is my dataType attribute right in jQuery ajax function? (I guess it should be JSONP
data.Data[0].Title
.