【问题标题】:500 Internal Server Error JsonResult mvc asp.net500 内部服务器错误 JsonResult mvc asp.net
【发布时间】:2016-01-06 17:30:51
【问题描述】:

尝试使用 jsonresult 和 ajax 创建级联下拉菜单,但我不明白为什么会出现 500 内部服务器错误。错误发生在以下方法之上:

    [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

【问题讨论】:

  • 您能粘贴错误描述吗?它将在错误回调中可用,甚至可以使用开发者工具。
  • 另外,您可以在操作中放置断点以查看失败的位置
  • 您可以将方法声明中的string brandID="" 替换为int brandID。然后您可以删除此控件:int.TryParse(brandID, out ID)。也许它会解决问题,但可能不会。可以试试吗?

标签: c# json ajax asp.net-mvc


【解决方案1】:

您需要将您的 return 语句移动到 using 块内 在执行 return 语句之前处理 Db 上下文

public JsonResult GetModels(int brandID)
{
    List<Model> models = new List<Model>();

    using (CarsEntities1 dc = new CarsEntities1())
    {
       models = dc.Models.Where(a => a.Brand_ID == brandID).OrderBy(a =>a.Model_name);
       if (Request.IsAjaxRequest())
       {
           return new JsonResult
           {
                Data = models.ToList(),
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
           };
        }  
    }

    return new JsonResult
    {
        Data = "Not valid request",
        JsonRequestBehavior = JsonRequestBehavior.AllowGet
    };    
}

【讨论】:

  • 试过了,但没有任何改变。
【解决方案2】:

json 返回类型必须是原始的,所以我相应地更改了代码(将 List 交换为 String[] 并将 OrderBy 交换为 Select:

public JsonResult GetModels(int brandID)
{
 String[] models;

using (CarsEntities1 dc = new CarsEntities1())
{
   models = dc.Models.Where(a => a.Brand_ID == brandID).Select(a=> a.Model_name).toArray();
   if (Request.IsAjaxRequest())
   {
       return new JsonResult
       {
            Data = models,
            JsonRequestBehavior = JsonRequestBehavior.AllowGet
       };
    }  
}

return new JsonResult
{
    Data = "Not valid request",
    JsonRequestBehavior = JsonRequestBehavior.AllowGet
};    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 2011-03-01
    • 2011-12-23
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多