views:

165

answers:

3

Hi,

I want to send an array of objects in the get request string. I know this isn't the optimal solution, but I really just want to get this up and running.

If I have a class, something like this

public class Data
{
   public int a { get; set; }
   public int b { get; set; }
}

public class RequestViewData
{
   public IList<Data> MyData { get; set; }
}

I thought I could bind the MVC route to a web request like this

http://localhost:8080/Request?MyData[0].a=1&amp;MyData[0].b=2&amp;MyData[1].a=3&amp;MyData[1].b=4

But all this does is create an array of two data objects without populating the values 1,2, 3 or 4.

Is there a way to bind complex objects arrays?

+1  A: 

I'd use BinaryFormatter to create a binary representation of my object, send it Base64 encoded via the querystring and reassemble it at the other end.

Jens
A: 

This blog is a little old, but it should show you how to do what you are asking:

http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

amurra
That's sending it as a POST, I'm trying to encode it in the URL. What confuses me is it's half-working.
Mr Snuffle
All the examples I've read about and used for array's of complex types is that it must be posted for the default model binder to work. You could always write your own custom model binder to do what you want.
amurra
+1  A: 

Assuming that you have implemented a method GetArrayTest in your HomeController

public class HomeController
{
    public ActionResult GetArrayTest (List<Data> data)

}

The following would work.

http://localhost:8080/Home/GetArrayTest?Data[0].a=1&amp;Data[0].b=1&amp;Data[1].a=2&amp;Data[1].b=2&amp;Data[2].a=3&amp;Data[2].b=3

Syd