views:

63

answers:

1

I have a complex ViewModel object being used in a base controller class. For simplicity it looks like this:

public class FruitBowl
{
    public Apple[] apples;
    public Banana[] bananas;
}

public class Apple
{
    public string appleType;
}

Now, if I called TryUpdateModel(myFruitBowlObj, "apples[0]"), the model binder will identify that it is the first apple in the array that should be updated from the POST data. Alternately, I could POST data to my controller where the form fields were named with the scheme "apples[0].appleType", and just call TryUpdateModel(myFruitBowlObj), and it would correctly update the first apple.

My question is - what is the easiest way to get a reference to apple[0] from myFruitBowlObj, given the prefix string "apple[0]"? I can follow how TryUpdateModel() does it, but it is complex and uses many protected methods. Is there some model binding logic that I can access somewhere that I'm overlooking, or do I need to do it manually via reflection?

+1  A: 

Manually using reflection is your best bet.

I tried to rip pieces out of the model binder before and it was always a case of "ok, now I need this, ok, now I need this, ok, now I need this" and I ended up ripping huge chunks out of the MVC source before just solving my problems with some manual reflection methods.

jfar
I'm rapidly coming to the same conclusion.
womp