Asp.net MVC Binding an Object to Properties, which are collections of objects that also have the properties of a child set

I am trying to get data from a massive form using Asp.net MVC. Its top object L1 contains properties that are collections of other L2 types. However, the L2 type contains some properties, which are collections of the L3 type, etc. There are probably 5 levels of nested collections.

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

However, when we have nested objects, it will be quite difficult / nightmare to go to town [0] .council [0] .street [0]. Name and use this convention to denote html elements.

Anyone can run into this situation / can see an elegant way to solve it?

thank

Mickey

+2


source to share


2 answers


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

There are many tutorials for creating model bindings, here are some of them:



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

+1


source


You might want to use LINQ to abstract the nitty gritty for you and allow you to do joins and whatnot on lists.



0


source







All Articles