0

I have a piece of javascript posting to my controller like this:

var url = '/Account/getCols';
var formData = { colName: colName }
$.post(url, formdata, function (data, textstatus) {

etc...

where Account is the controller getCols is a method within that controller. The method returns a list like so:

public List<string> getCols(string colName)
{
    //do some stuff here
    List<string> l = new List<string>;
    //do some more stuff like adding and other manipulation here
    return l;
}

How do I use the list when it is returned within my javascript? or should I use some returned Json? or anything else? Sorry, I am rather new to MVC and still looking APIs etc... Would be grateful for help.

1
  • 2
    Serializing it as JSON would be the natural way to go. Commented Jan 7, 2014 at 14:53

1 Answer 1

2

Serializing it as JSON would be the natural way to go. Something like this should do it, if getCols is a controller action:

public ActionResult getCols(string colName)
{
    //do some stuff here
    List<string> l = new List<string>;
    //do some more stuff like adding and other manipulation here
    return Json(l);
}

If the action is called through HTTP GET, you will have to allow that, since only POST is allowed by default. Something like this:

return Json(l, JsonRequestBehavior.AllowGet);
Sign up to request clarification or add additional context in comments.

Comments

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.