试图创建与jsonresult和ajax级联dropdownmenu,但我不明白为什么我得到500内部服务器错误。500内部服务器错误JsonResult mvc asp.net
[HttpGet]
public JsonResult GetModels(string brandID="")
{
List<Model> models = new List<Model>();
int ID = 0;
if (int.TryParse(brandID, out ID))
{
using (CarsEntities1 dc = new CarsEntities1())
{
models = dc.Models.Where(a => a.Brand_ID == ID).OrderBy(a =>a.Model_name).ToList();
}
}
if (Request.IsAjaxRequest())
{
return new JsonResult
{
Data = models,
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
else
{
return new JsonResult
{
Data = "Not valid request",
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
}
我使用的方法来传递项目的列表为DropDownMenu和下面的代码试图输出列表:
$(document).ready(function() {
//if (typeof ($) == 'function') alert('jQuery is loaded.');
$("#brand_Brand_ID").change(function() {
// this will call when Brand Dropdown select change
var brandID = parseInt($("#brand_Brand_ID").val());
if (!isNaN(brandID)) {
var ddModel = $("#Model_ID");
ddModel.empty(); // this line is for clear all items from Model dropdown
ddModel.append($("<option></option").val("").html("Select model"));
// Here I will call Controller Action via Jquery to load Model for selected Brand
$.ajax({
url: "@Url.Action("GetModels","ModelSpec")",
type: "GET",
data: { brandID: brandID },
dataType: "json",
success: function (data) {
if (data != null && data.success) {
$.each(data, function (i, val) {
ddModel.append(
$("<option></option>").val(val.Model_ID).html(val.Model_name)
);
});
}
},
error: function() {
alert("Fail");
}
});
}
});
});
我得到的是上面下面的方法出现错误以下: GET http://localhost:2508/ModelSpec/GetModels?brandID=2 500内部服务器错误的jquery-1.7.1.js(线8102)
此外,我注意到穿过GetModels方法世界上没有数据时犯规发生错误。有时我得到: GET/ModelSpec/GetModels?brandID = 5 401未授权
只要GetModels返回任何错误发生,否则不会。
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection
堆栈跟踪: http://pastebin.com/3aXg7YiM
您能否粘贴错误描述?它将在错误回调中提供,甚至可以使用开发人员工具。 – gustavodidomenico
另外,您可以在操作中放置断点以查看其失败的位置 –
您可以用'int brandID'在方法声明中替换'string brandID =“”'。然后你可以删除这个控件:'int.TryParse(brandID,out ID)'。也许它会解决问题,但可能不会。你可以试试吗? – erikscandola