Javascript, MVC Controller caling and return param

2019-09-09 05:06发布

I am able to call a controller method from Javascript. The controller method has Actionresult as return type. Can someone explain, how to return a populated ArrayList from the called controller method, to the calling javascript? How should the javascript handle the returned arraylist?

Regards, Anil

2条回答
ら.Afraid
2楼-- · 2019-09-09 05:29

return your arraylist as...

return Json(arraylist);

then iterate through like an object array

function(result) {
    $.each(result, function(i, item){
        alert(item.title + " : " + item.key);
    });
查看更多
欢心
3楼-- · 2019-09-09 05:38

You could change the Controller to return a JsonResult instead of an ActionResult, and then JSON-encode your arraylist. I guess that would be the easiest way to go about it.

public JsonResult YourAction () {

   // ... DO your stuff

   return Json(yourArrayList);
}

Here is a ref to the documentation of Json().

Your JavaScript would then have to parse the JSON. The easiest way, if you are familiar with it is probably to use jQuery, but there are other ways to go about it.

查看更多
登录 后发表回答