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
-
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.David– David2013年07月18日 15:40:26 +00:00Commented Jul 18, 2013 at 15:40
-
1Have you tried the alternative solutions form here: stackoverflow.com/questions/9981330/…?nemesv– nemesv2013年07月18日 15:44:49 +00:00Commented Jul 18, 2013 at 15:44
1 Answer 1
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
haim770
You could also use
String.Split
instead of the JavaScriptSerializer
.lang-cs