问题
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