Javascript, MVC Controller caling and return parameters

情到浓时终转凉″ 提交于 2019-12-12 04:57:37

问题


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


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/9990710/javascript-mvc-controller-caling-and-return-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!