I have an ASP.net MVC controller called Designs
that has an action with the following signature:
public ActionResult Multiple(int[] ids)
However, when I try to navigate to this action using the url:
http://localhost:54119/Designs/Multiple?ids=24041,24117
The ids
parameter is always null. Is there any way to get MVC to convert the ?ids=
URL query parameter into an array for the action? I've seen talk of using an action filter but as far as I can tell that will only work for POSTs where the array is passed in the request data rather than in the URL itself.
You can also use this URL format, and ASP.NET MVC will do everything for you. But, remember to apply URL encoding.
The default model binder expects this url:
in order to successfully bind to:
And if you want this to work with comma separated values you could write a custom model binder:
and then you could apply this model binder to a particular action argument:
or apply it globally to all integer array parameters in your
Application_Start
inGlobal.asax
:and now your controller action might look like this:
I don't know where Groky's URL string was coming from, but I had the same problem with some javascript calling my controller/action. It would build up a URL of
null
, 1, or many "IDs" from a multiple-select list (which is unique to the solution I'm going to share).I copy/pasted Darin's custom model binder and decorated my action/parameter, but it didn't work. I still got
null
valuedint[] ids
. Even in the "safe" case where I actually did have many IDs.I ended up changing the javascript to produce an ASP.NET MVC friendly parameter array like
I had to do some silly stuff, though
So, the full block was
It's messy, but it's the first time I had to hack like this in javascript.
To extend on Darin Dimitrov's answer, something you can get away with is accepting a simple
string
in your URL parameter and converting it to an array yourself:If you get a parse error while doing this (because someone passed you a malformed array), you can cause your exception handler to return a
400 Bad Request
error instead of the default, more unfriendly404 Not Found
error that MVC returns when an endpoint is not found.