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?

1 Answer 1

3

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);
         });
Sign up to request clarification or add additional context in comments.

4 Comments

@James Kolpack - like this ? public ActionResult GetList(int id) { int[] list = new int[] {1, 2, 4}; return Json(list); }
@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
The JsonRequestBehavior.AllowGet might be ASP.NET-MVC2 specific, perhaps?
Yes, AllowGet is new for MVC2 .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.