Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display JSON Array Data Returned from Controller in Asp.Net MVC

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; enter image description here 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

like image 615
Idrees Khan Avatar asked Apr 13 '26 22:04

Idrees Khan


1 Answers

try this:

data.Data[0].Title

This should be working

like image 61
radu florescu Avatar answered Apr 16 '26 23:04

radu florescu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!