Eliminating indexes in collections in ASP.NET MVC

As an initial blog post will bring a relatively advanced topic, the question of Bind collections in ASP.NET MVC. There are two way to retrieve nested collections in the view controller, the first a sequential index is used:

Fields in the view:



Recovery in the controller:



The second form allows you to use a non-sequential alphanumeric index:



In this second example is necessary to include a field of type "hidden" for each input field value with the value being the index. Although this second option being more versatile is limited because the index could still be single.

After much study and research I saw that it was possible to create a custom bind, then why not create one to eliminate the indexes and retrieve the collections?
In the bind is possible to have access to the form that was submitted, it allowed access to all properties of the form and its values​​:



will not delve into the bind code, it is available for study and design improvements: https://github.com/rafaelguinho/collectionbinder. To use copy the class Binders folder to your project Global.asax.cs and add a line for each collection:



In his view the collections can now have all the same name, which should be "NametheCollection.Attribute" as example:



In the next post I'll show how to "automate" the process of adding and removing using a Helper. Even more!

0 comentários:

Postar um comentário