vendredi 24 juillet 2015

MVC Controller Param using list of objects. Odd behaviour

So I was wondering if I'm missing something here or if MVC just can do this. Say I have this class:

public class Stuff
{
    public string Thing1 { get; set; }
    public string Thing2 { get; set; }
    public string Thing3 { get; set; }
}

And this is used in my controller like so:

private ActionResult Something(List<Stuff> stuff)...

Now how come when I execute this link:
.../#Tester/Something?Thing1=hello&Thing2=world&Thing3=WachuUpTo
The url params don't bind to the controllers List of items. But if I reconfigure my class to look like this:

public class Stuff
{
    public List<string> Thing1 { get; set; }
    public List<string> Thing2 { get; set; }
    public List<string> Thing3 { get; set; }
}

Then make the controller param a single object:

private ActionResult Something(Stuff stuff)...

It works. Thing1, Thing2, Thing3 each have one element in the list with the correct values in each. Now why does this way work but the other doesn't?
I'd much prefer this first way because it's simpler since I don't have to worry about adding logic to make sure the number of elements in each sublist are the same. Working with MVC 3 btw. Also I'm trying to do this using url params if possible.

Aucun commentaire:

Enregistrer un commentaire