R.C
R.C

Reputation: 10565

MVC controller not returning JSON data?

I was returning JSON data of a Product from a controller JsonDetailsProduct. This is in turn called using the Jquery.

However, the success function of the Jquery call doesn't seems to be receiving any data. I guess Something is wrong within my controller.

MVC Controller:

    public ActionResult JsonDetailsProduct(int id)
{ 
    Product pdt = NWDC.GetProduct(id); 
    if (pdt == null)
 { 
    // i'm throwing a custom exception here
    throw new RecordNotFoundException(); 
 } 
else
 { 
    return Json(new { 
    ProductId = prod.ProductID, 
    ProductName = prod.ProductName, 
    UnitPrice = prod.UnitPrice, 
    UnitsInStock = prod.UnitsInStock, 
    Discontinued = prod.Discontinued 
    }, JsonRequestBehavior.AllowGet); 
  } 
} 

the JQuery call:

<script type="text/javascript">
$(document).ready(
  function(){   
    alert("About to make the call"); // just added it for my debugging purpose
   $.getJSON( "/SlimProductServices/JsonDetailsProduct",  
               data:{id:$('#txtProductID').val()},
    function(productData)
    {
      alert(productData);
    }
    );
 }
);

Any thoughts where things are setup wrongly and whether any issue in Jquery Call also present ??

EDIT Also, I corrected the data parameter of my Jquery call, but the first Alert box shows up after that blank pop ups.

Upvotes: 0

Views: 2910

Answers (2)

Shubh
Shubh

Reputation: 6731

Not sure if this solves your issue, I just gave a simple run with your code.

Here is what I have tried:-

public ActionResult JsonDetailsProduct(int id)
{
    Product pdt = GetProduct(id);
    if (pdt == null)
    {
        // i'm throwing a custom exception here
        throw new RecordNotFoundException();
    }
    else
    {
        return Json(new Product //Comment:-Added the Object Type
        {
            ProductId = pdt.ProductId,//Comment:-Sure if its 'prod' and not pdt ?
            ProductName = pdt.ProductName,
            UnitPrice = pdt.UnitPrice,
            UnitsInStock = pdt.UnitsInStock,
            Discontinued = pdt.Discontinued
        }, JsonRequestBehavior.AllowGet);
    }
}

Two changes which I have made mentioned it as Comment:-, please have a look.

Secondly, I have tried with $.ajax , for which the code is as follows:-

<script type="text/javascript">
$(document).ready(function () {
    $.ajax({
        url: '/SlimProductServices/JsonDetailsProduct/'+$('#txtProductID').val(),
        type: 'GET',
        //data: $('#txtProductID').val(), //You can un-comment it later
        success: function (datos) {
            console.log(datos);
        },
    });
});
</script>

I am getting proper values returned from my Action Method. Just try it out.

Upvotes: 1

Christian Phillips
Christian Phillips

Reputation: 18769

You're missing the id?

$.getJSON( "/SlimProductServices/JsonDetailsProduct", { 'id': 1 },...

public ActionResult JsonDetailsProduct(int id)

Take a look at getJson in the jquery docs.

Where are you getting the ID from? If you just want to test the call without changing your jquery call, you could add a second Action method...

public ActionResult JsonDetailsProduct()
{ 
    Product pdt = NWDC.GetProduct(1); //hard code product Id that exists for testing.

Upvotes: 1

Related Questions