Ajax Get中的500错误 [英] 500 error in Ajax Get

查看:45
本文介绍了Ajax Get中的500错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向控制器发送一个下拉列表选择的值,并取回json数据.

I'm trying to send a dropdownlists selected value to controller and retrieve json data back.

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

When I debug, parameter gets to controller nicely, values are retrieved nicely but after return part on console it says

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

"Failed to load resource: the server responded with a status of 500 (Internal Server Error)`"

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

This is my controller action: (in WebContentsController)

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

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

this is JS part (printing it to console for testing)

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

WebContentTypeDetail模型

WebContentTypeDetail model

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模型

WebContentType model

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

推荐答案

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

You cannot directly return a entity list from context query result, because they are not serializable in most of cases, especially in your case: entities got a loop references.

您有2个选择:

var details = db.WebContentTypeDetail.Where(x=>x.WebContentTypeID == id)

  1. 将您的结果映射到匿名对象列表:

  1. map your result to anonymous object list:

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

  • 创建您的视图模型并将其标记为 [Serializable] ,然后将必要的数据返回给客户端:

  • create your view model and mark it [Serializable], then return necessary data to client:

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

  • 这篇关于Ajax Get中的500错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆