Javascript, Mvc Controller Caling And Return Parameters
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 call
Solution 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);
});
Solution 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 stuffreturn 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.
Post a Comment for "Javascript, Mvc Controller Caling And Return Parameters"