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?
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?
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)
String.Split instead of the JavaScriptSerializer.