Mickey
Mickey

Reputation:

Asp.net MVC Binding an object with properties which are collections of objects which also have child collection properties

I'm trying to retreive data from a massive form using Asp.net MVC. Its top object L1 contains properties which are Collections of other types L2. However the Type L2 contains some properties which collections of type L3, and so on. There are probably 5 levels of nested collections.

I've seen the approach of binding to Lists in Asp.Net MVC, where the element name has an array substring included in the names of all its propertie html elements, e.g. [0] in the first set, [1] in the second set etc.

However when we've got nested objects, its going to be quite tricky / nightmare to go town[0].council[0].street[0].Name and use that convention to name the html elements.

Has anyone come across this situation / can see an elegant way to resolve it?

Thanks

Mickey

Upvotes: 2

Views: 1141

Answers (2)

Abram Simon
Abram Simon

Reputation: 3269

The default model binder that ships with ASP.NET MVC is what dictates the form element naming convention you are referring to. If there is another convention that you would like to use to name the form elements, go for it. Then you just have to write a custom model binder that can populate your nested objects based on your convention.

There are lots of tutorials out there for creating model binders, here are some:

http://www.singingeels.com/Articles/Model_Binders_in_ASPNET_MVC.aspx

http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/03/17/a-better-model-binder.aspx

Upvotes: 1

James
James

Reputation: 6509

You may want to consider using LINQ to abstract the nitty gritty for you and allow you to perform joins and whatnot on the lists.

Upvotes: 0

Related Questions