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?