简体   繁体   中英

How to pass Json array from a controller to Javascript (ajax) in view.cshtml

I'm trying to pass a array of Document objects from a method (let's say from a controller) to the JavaScript from which the method is called (in a view). For some reason, the method won't pass the array (it works only if the array is empty). I also tried to put all the array members into a new array, even a list and pass it to the view, but it won't work (Inspect Element -> Console = 500 (Internal Server Error)) ( Screenshot here ).

Here is a part of the JavaScript code:

$.ajax({
            method:"get",
            data: data,
            url: url + "?binderId=" + companyId +"&description="+ data

        }).success(function (response) {
            console.log("Success");
            console.log(response);

        }).error(function (response) {
            console.log("Error");
            console.log(response);
        });

Here is the C# code, sending the array to the JavaScript from above:

 public ActionResult SearchDocument(int binderId, string description)
    {
        //documents is a array
        var documents = Search.SearchDocument(binderId, description);
        return Json(documents, JsonRequestBehavior.AllowGet);
    }

I think that somethings wrong with the JavaScript, but I don't know what. Thank you!

Try returning JsonResult instead of ActionResult

public JsonResult SearchDocument(int binderId, string description)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM