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?

asked Jul 18, 2013 at 15:36
2
  • This might be helpful: stackoverflow.com/questions/6243051/… 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. Commented Jul 18, 2013 at 15:40
  • 1
    Have you tried the alternative solutions form here: stackoverflow.com/questions/9981330/…? Commented Jul 18, 2013 at 15:44

1 Answer 1

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)
answered Jul 18, 2013 at 16:08
Sign up to request clarification or add additional context in comments.

1 Comment

You could also use String.Split instead of the JavaScriptSerializer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.