In ASP.NET MVC 2, is it possible to deserialize a request in an array using the standard ModelBinder?

In ASP.NET MVC 2, you can use this URL and this controller method:

GET http://server/controller/get?id=5 public ActionResult Get(int id) { ... } 

And ModelBinder will convert querystring id=5 to id = (int) 5 in the method parameter. However, this will not work:

 GET http://server/controller/get?idlist=1,2,3,4,5 public ActionResult Get(int[] idlist) { ... } 

idlist will be invalid in the parameter. Although parsing is pretty trivial for this, I was wondering if there is a way to change the method signature or query sequence to make ModelBinder default to automatically deserialized arrays / collections?

+4
source share
3 answers

When using the default template, the URL should be

 http://server/controller/get?idlist=1&idlist=2&idlist=3&idlist=4&idlist=5 

or

 http://server/controller/get?idlist[]=1&idlist[]=2&idlist[]=3&idlist[]=4&idlist[]=5 

If you really want to use idlist = 1,2,3,4,5 you must have your own binding

+8
source

It's a bit late for the party, but I wanted to do the same.

You can pass one string as {1,2}, and the binder will associate it with an array for example

 <input name="idlist" type="text" value="{163,162}"> 
-1
source

That's what I think:

 public ActionResult Get(int id) { ... } 

Should be used as

 GET http://server/controller/get/5 

And the list of identifiers can simply be separated by a comma (,)

-4
source

All Articles