daniel
daniel

Reputation: 35683

ASP.NET MVC 5 Controller JsonResult Internal Server Error

Why do I get a 500 Internal server error?

C#

 public JsonResult GetCategory(string id)
        {
                long eocategoryid = Convert.ToInt64(id);
                dbEntities db = new dbEntities();
                ttCategory cat = db.ttCategories.First(x => x.ID == eocategoryid);

                return Json(new
                {
                    catgeory = cat
                }, JsonRequestBehavior.AllowGet);

 }

JS:

 $.ajax({
                     type: "GET",
                     url: "/GetCategory",
                     data: { id: data.node.a_attr.id },
                     datatype: "json",
                     success: function (data) {
                         console.log(data);
                    }
                 });

Upvotes: 0

Views: 2919

Answers (1)

Triet Doan
Triet Doan

Reputation: 12085

It seems that the problem lies in the serialization process. I think ttCategory is an auto-generated class from Entity Framework. Don't try to serialize the whole class. Only take the fields you need and return it to the client.

Example return:

return Json(new
                {
                    firstName = cat.FirstName,
                    lastName = cat.LastName,
                }, JsonRequestBehavior.AllowGet);

Upvotes: 1

Related Questions