1

This seems simple enough but i am not able to get past the error i am receiving while creating cascading drop downs. I have visited this questions made changes from type: "GET" to type: "post", also in controller method i tried JsonResult and ActionResult. Everything seems right but still the error remains. The questions i checked are :this,this,this,this and this plus many tutorials and questions in other forums.

I am using VS2013 community with localdb and iis express

Following is my model:

public class ItemPrice
{
    public int ItemPriceID { get; set; }
    public int ItemID { get; set; }
    public int price { get; set; }

    public virtual Item item { get; set; }
}

the controller action method is:

public JsonResult GetPriceByItemId(string ItemId)
    {
        int Id = Convert.ToInt32(ItemId);

        var prices = (from a in db.itemprice where a.ItemID == Id select a);

        return Json(prices, JsonRequestBehavior.AllowGet);
    }

when debugging i made sure that the control is transferred at this method and query executes successfully.

the script:

< script >
  $(function() {

    $('#ItemID').change(function() {

      $('#price').empty();

      $.ajax({
        type: "GET",
        url: '@Url.Action("GetPriceByItemId", "StockIns")',
        datatype: "Json",
        data: {
          ItemId: $('#ItemID').val()
        },
        success: function(data) {
          $.each(data, function(index, value) {
            $('#price').append('<option value="' + value.ItemPriceID + '">' + value.price + '</option>');
          });
        }
      });
    });
  }); < /script>

i have debugged script in chrome developer tools and nothing looks wrong there. The error appears when loops in script starts to execute(or just before the execution of the loop.)

Community
  • 1
  • 1
Gaurav Chauhan
  • 1,295
  • 4
  • 17
  • 41
  • 2
    `return Json(prices.ToList(), JsonRequestBehavior.AllowGet);` – Satpal Oct 14 '16 at 09:06
  • A 500 error means a server side problem, so you should set a breakpoint in the Action and debug that too. – Rory McCrossan Oct 14 '16 at 09:07
  • @Satpal i tried it. In this case the loop in ajax execute once and i get undefined in dropdown. – Gaurav Chauhan Oct 14 '16 at 09:10
  • @Rory McCrossan i debugged action in controller. No error and there i get expected results in var prices. – Gaurav Chauhan Oct 14 '16 at 09:10
  • Then are you sure it's this AJAX request that's causing the problem. You can't have a 500 error and return data from the same request – Rory McCrossan Oct 14 '16 at 09:14
  • Seems that `$.each(data, function(index, value)` causing the error on client-side, assuming `price` is a drop down/select list. Also, you may include error element in ajax call to raise error into the page. – Tetsuya Yamamoto Oct 14 '16 at 09:18
  • Debug your code. Open your browser tools (Network Tab) and inspect the response t see the details of the error. (and change the method parameter to `int ItemId` and send back an anonymous object containing only what you need, not the every property of the model - `var data = prices.Select(x => new { ItemPriceID = x.ItemPriceID, price = x.price }); return Json(data, ....);`) –  Oct 14 '16 at 10:01
  • @RoryMcCrossan data is returned from controller but somehow at view i am not able to get data. – Gaurav Chauhan Oct 14 '16 at 10:23
  • If your getting a `500(Internal Server Error)` the data is **not** being sent despite what you think. Use your browser tools to debug your code! –  Oct 14 '16 at 10:36

0 Answers0