0

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

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

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

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

EDIT:

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 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; }
    }
Ege Bayrak
  • 1,139
  • 3
  • 20
  • 49
  • You need to show the model for `WebContentTypeDetail` and any models that it also contains –  Jun 23 '16 at 07:50
  • Are you able to run the debugger when doing the request? What is in the details variable? – counterflux Jun 23 '16 at 07:53
  • Details returns exactly what I need, that part is allright – Ege Bayrak Jun 23 '16 at 07:54
  • 2
    The issue is that `WebContentTypeDetail` contains a property which is typeof `WebContentType` and `WebContentType` contains a proeprty which is a collection of `WebContentTypeDetail` which creates a circular reference exception. As noted in your previous question, you need to return only those properties you need in the view (using a collection of a view model or anonymous objects) –  Jun 23 '16 at 08:06
  • How can I do it ? I came across this question and accepted answer looks promising http://stackoverflow.com/questions/1153385/a-circular-reference-was-detected-while-serializing-an-object-of-type-subsonic – Ege Bayrak Jun 23 '16 at 08:13
  • Also [this answer](http://stackoverflow.com/questions/25402894/return-jsonresult-with-list-of-objects-from-mvc-controller/25403595#25403595). We cant give you the code because we don't know which properties you need in the view. –  Jun 23 '16 at 08:16

2 Answers2

1

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.

you have 2 options:

var details = db.WebContentTypeDetail.Where(x=>x.WebContentTypeID == id)
  1. map your result to anonymous object list:

    return Json(details.select(x=>new {
      //you data structure
    }).ToList(), JsonRequestBehavior.AllowGet);
    
  2. 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);
    
Dongdong
  • 2,208
  • 19
  • 28
-1

Try this:

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

As parameter id should be passed as querystring. And if you're using mvc then url should be passed in @url.action so it must be

$(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);
    })
});
});
Shilpa Soni
  • 2,034
  • 4
  • 27
  • 38
  • the issue is not in routing, 500 means: the request arrives right location, but server side code did not work. and if the scripts is in xxx.js file, your razor code won't work as well. and `Url.Action` return action result, not a url, it should be `Url.ActionLink`. and never try to do your first code, it won't pass code review. – Dongdong Jul 19 '18 at 19:30