【问题标题】:500 error in Ajax GetAjax 获取 500 错误
【发布时间】:2016-06-23 07:47:53
【问题描述】:

我正在尝试将下拉列表中的选定值发送到控制器并取回 json 数据。

当我调试时,参数很好地到达控制器,值很好地检索,但是在控制台上返回部分后它说

"加载资源失败:服务器响应状态为 500 (Internal Server Error)`"

这是我的控制器操作:(WebContentsController

[HttpGet]
public JsonResult GetWebContentTypeDetails (int id)
{
    var details = db.WebContentTypeDetail.Where(x=>x.WebContentTypeID == id).ToList();
    return Json(details, JsonRequestBehavior.AllowGet);
}

这是 JS 部分(打印到控制台进行测试)

$(document).ready(function () {
    $("#WebContentTypeID").change(function () {
        var ChangedID = $('#WebContentTypeID option:selected').val();
        alert(ChangedID);
        $.getJSON('/webcontents/GetWebContentTypeDetails/' + ChangedID, function (data) {
            console.log(data);
        })
    });
});

编辑:

WebContentTypeDetail 模型

public partial class WebContentTypeDetail
    {
        public int WebContentTypeDetailID { get; set; }
        public int WebContentTypeID { get; set; }
        public string DetailKey { get; set; }
        public string Description { get; set; }
        public Nullable<short> Rank { get; set; }

        public virtual WebContentType WebContentType { get; set; }
    }

WebContentType 模型

public partial class WebContentType
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public WebContentType()
        {
            this.WebContent = new HashSet<WebContent>();
            this.WebContentTypeDetail = new HashSet<WebContentTypeDetail>();
        }

        public int WebContentTypeID { get; set; }
        public string DisplayName { get; set; }
        public string CreatedByUserID { get; set; }
        public System.DateTime CreateDate { get; set; }
        public string LastEditedByUserID { get; set; }
        public Nullable<System.DateTime> LastEditDate { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<WebContent> WebContent { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<WebContentTypeDetail> WebContentTypeDetail { get; set; }
    }

【问题讨论】:

  • 您需要显示WebContentTypeDetail 的模型以及它还包含的任何模型
  • 在执行请求时是否可以运行调试器? details 变量中有什么?
  • Details 返回的正是我需要的,那部分没问题
  • 问题是WebContentTypeDetail 包含一个类型为WebContentType 的属性,WebContentType 包含一个属性,它是WebContentTypeDetail 的集合,它创建了一个循环引用异常。如您上一个问题所述,您只需要返回视图中需要的那些属性(使用视图模型或匿名对象的集合)
  • 我该怎么做?我遇到了这个问题,接受的答案看起来很有希望stackoverflow.com/questions/1153385/…

标签: ajax asp.net-mvc


【解决方案1】:

您不能直接从上下文查询结果中返回实体列表,因为它们在大多数情况下是不可序列化的,尤其是在您的情况下:实体有循环引用。

你有两个选择:

var details = db.WebContentTypeDetail.Where(x=>x.WebContentTypeID == id)
  1. 将您的结果映射到匿名对象列表:

    return Json(details.select(x=>new {
      //you data structure
    }).ToList(), JsonRequestBehavior.AllowGet);
    
  2. 创建您的视图模型并将其标记为[Serializable],然后将必要的数据返回给客户端:

    return Json(details.select(x=>new YourViewModel {
     //you data structure
    }).ToList(), JsonRequestBehavior.AllowGet);
    

【讨论】:

    【解决方案2】:

    试试这个:

    $(document).ready(function () {
    $("#WebContentTypeID").change(function () {
        var ChangedID = $('#WebContentTypeID option:selected').val();
        alert(ChangedID);
        $.getJSON('/webcontents/GetWebContentTypeDetails?id=' + ChangedID, function (data) {
            console.log(data);
        })
    });
    });
    

    作为参数 id 应该作为查询字符串传递。如果您使用的是 mvc,那么 url 应该在 @url.action 中传递,所以它必须是

    $(document).ready(function () {
    $("#WebContentTypeID").change(function () {
        var ChangedID = $('#WebContentTypeID option:selected').val();
        alert(ChangedID);
     var URL = @Url.Action("_GetWebContentTypeDetails", "webcontents");
    
        $.getJSON(URL + '?id=' + ChangedID, function (data) {
            console.log(data);
        })
    });
    });
    

    【讨论】:

    • 问题不在于路由,500 表示:请求到达正确的位置,但服务器端代码不起作用。如果脚本在 xxx.js 文件中,您的剃须刀代码将无法正常工作。而Url.Action返回动作结果,不是url,应该是Url.ActionLink。永远不要尝试编写你的第一个代码,它不会通过代码审查。
    猜你喜欢
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-04
    相关资源
    最近更新 更多