views:

130

answers:

1

Here is my jQuery code:

 $.get('/Home/GetList', function(data) {
             debugger;
             $('#myMultiSelect').val(data);
         });

Here is my controller code:

    public ActionResult GetList(int id)
    {
        int[] bodyParts = _repository.GetList(id);

       //how do i return this as an array back to javascript ??
    }

if I have the GetList function return an array of integers, how do I return this to the jQuery function?

+2  A: 

Return it as a JsonResult instead of ActionResult, which javascript can easily deal with. See a blog article here.

This will look something like:

public JsonResult GetList(int id)
{
   int[] bodyParts = _repository.GetList(id);

   return this.Json(bodyParts);
}

Then use getJSON() to retrieve it:

 $.getJSON('/Home/GetList', null, function(data) {
             debugger;
             $('#myMultiSelect').val(data);
         });
James Kolpack
@James Kolpack - like this ? public ActionResult GetList(int id) { int[] list = new int[] {1, 2, 4}; return Json(list); }
ooo
@James Kolpack - i figured out what was going wrong. you got me close but there is one more thing you need to add in the this.json) you have to add one more argument JsonRequestBehavior.AllowGet); please update this and i will accept your answer
ooo
The `JsonRequestBehavior.AllowGet` might be ASP.NET-MVC2 specific, perhaps?
James Kolpack
Yes, AllowGet is new for MVC2 .
Cheeso
Ah thanks, good to know.
James Kolpack