3

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

9
  • what's the url of your page (where the javascript is) and the url of your mvc site ? Are they on the same domain + port ? Commented Apr 6, 2013 at 7:25
  • yeah, on the same doamin/site. Commented Apr 6, 2013 at 7:26
  • see the var serviceBase = "/catalog/"; i updated the question Commented Apr 6, 2013 at 7:27
  • It looks like it's an object with the property Data which is an array of objects. Try data.Data[0].Title. Commented Apr 6, 2013 at 7:29
  • Try to stringify the whole return to see what's in: alert(JSON.stringify(data)) Commented Apr 6, 2013 at 7:29

3 Answers 3

4

try this:

data.Data[0].Title

This should be working

Sign up to request clarification or add additional context in comments.

2 Comments

yeah this works, coz i was wrapping up everything in Data. And also it's an array. So, i should tried this.
no, just custom jquery. i used that before but now trying to do some custom coding. Anyway, thanks buddy
0

Try

alert(data[0].Title)

It's an array.

To display all you could do something like this:

$.each(data, function(index, value) {
    $('#somediv').append(value.Title);
});

4 Comments

TypeError: data[0] is undefined
and if you inspect data in chrome? Try putting a debugger; statement in your succes function and inspect it with firebug or chrome.
Thanks for downvoting me. Was my answer that bad?
if you have succes: function(data) then data is NOT with an uppercase D. Mo##$ns
0

I think you need to use JSON.stringify to send arguments(in your ajax call to controller. Something like
data: JSON.stringify({ Id: categoryId, page: page }),
Are you getting Id and page values at controller end properly? I think your result of ajax call is coming as null i.e. data = null at client end.

3 Comments

this is working fine because see the pciture. it's returning the data
Is it the complete string that you get in output? Don't you see it as JSON object in debugger mode?
the prob is sovled. i should use this data.Data[0].Title

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.