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.)