3

I know how to pass arrays to Get function like this: /?index=1&index=5&index=3

But I need to be able to receive arrays like this: /?index=[1,5,3]

Or something similarly short. Is there anything I can use?

bobby
  • 617
  • 1
  • 8
  • 19
  • This might be helpful: http://stackoverflow.com/questions/6243051/how-to-pass-an-array-within-a-query-string One way to test it would be to create a form with a GET action with a multi-select, select multiple options, and see how it formats the request and how the server interprets it. – David Jul 18 '13 at 15:40
  • 1
    Have you tried the alternative solutions form here: http://stackoverflow.com/questions/9981330/how-to-pass-an-array-of-integers-to-a-asp-net-web-api-rest-service?rq=1? – nemesv Jul 18 '13 at 15:44

1 Answers1

2

Use a custom ModelBinder:

public class JsArrayStyleModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

        if (value == null)
            return null;

        return new JavaScriptSerializer().Deserialize<string[]>(value.AttemptedValue);
    }
}

Then register it in your Global.asax:

ModelBinders.Binders.Add(typeof(string[]), new JsArrayStyleModelBinder());

Or directly on your Action parameter:

[HttpGet]
public ActionResult Show([ModelBinder(typeof(JsArrayStyleModelBinder))] string[] indexes)
haim770
  • 48,394
  • 7
  • 105
  • 133