5

I am trying to call a .net Web Api method via jquery's $.ajax. When I pass in

var process = "first process", var nameArray = ["first", "second"], var valueArray = ["val1", "val2"]

and then:

 $.ajax({
url: jsonFeed,
data: {process: process, nameArray: nameArray, valueArray: valueArray},
    etc...

I have a ASP.NET Web Api method:

  public string GetResponseToken(string process, string[] nameArray, string[] valueArray)

When I run everything, I receive an error message:

"Can't bind multiple parameters ('nameArray' and 'valueArray') to the request's content."

Does anyone know why this is, or how I can fix it to accept my arrays?

1
  • What is type of your ajax call GET or POST? Commented Oct 17, 2012 at 18:49

1 Answer 1

11

The Web.API parameter/model binder work differently than the MVC one. You need to tell it that you wan't to bind all of your arguments from the query string with the [FromUri] attribute:

public string GetResponseToken(
      [FromUri]string process, 
      [FromUri]string[] nameArray, 
      [FromUri]string[] valueArray)
{
    //... 
}

In the long run (e.g the above mentioned approach won't work if you your request type is a POST) you should consider to use a parameter object instead of having multiple arguments.

public string GetResponseToken([FromUri]ResponseTokenRequest request)
{
    //...
}

public class ResponseTokenRequest
{
    public string Process { get; set; }
    public string[] NameArray { get; set; }
    public string[] ValueArray { get; set; }
}

You can learn about the Wep.API parameter binding in the following articles:

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.