Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - handling multiple objects in one form

Tags:

asp.net-mvc

I have a scenario I'm stuck on - I have a domain object that has a collection of objects attached to it. Something like this:

public class Person
{
   public string Name { get; set; }
   public IList<PhoneNumber> PhoneNumbers {get; set; }
   public IList<Address> Addresses { get; set; }
}

The UI the client wants has a single input form for adding and editing. A user could enter 0 to many phones/addresses for each person. How do I handle posting the collection of values back to the controller?

I can think of a couple of approaches, but they all seem brute-force and not very elegant. Is there a best practice for handling this sort of problem?

like image 946
Jason Avatar asked Oct 26 '22 06:10

Jason


1 Answers

It is supported by the framework by using a special "form layout". Phil Haack has an article on this, check this out

Edit Scott Hanselman (http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx) just posted an update on this. Furthermore in RC1 it seems (ran into this mysel last night) that the indexes needs to be 0-based and steadily increasing (at least if you are "binding" against IList)

Edit2 Link didn't seem to work

like image 142
veggerby Avatar answered Nov 11 '22 15:11

veggerby