【发布时间】: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