Ajax Get中出现500错误

时间:2016-06-23 07:47:54

标签: ajax asp.net-mvc

我正在尝试向控制器发送下拉列表选定值并返回json数据。

当我调试时,参数很好地进入控制器,很好地检索了值但是在控制台上返回部分后它会显示

  

“无法加载资源:服务器响应状态为500(内部服务器错误)”

这是我的控制器操作:(在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; }
    }

2 个答案:

答案 0 :(得分:1)

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

您有2个选择:

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

答案 1 :(得分:-1)

试试这个:

$(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);
    })
});
});