MVC3 Models with repeating fields

How do you handle views that are strongly typed to models with repeating fields in MVC3? I would like to use the standard MVC3 methods of validation and use the HTML helper methods for generating inputs but have had to fall back on hand coding the inputs in HTML and using FormCollection for params in the controller method.

The representation of the email[] field in the view is basically a repeating text input (the user can click a button to add a new field). For existing emails (update view) it needs to pass the id and the address string. I have this working but it doesn't feel right at all.

public class Email
{
    public int EmailId
    public string EmailAddress
}

public class Person
{
    public int PersonId;
    [Required(ErrorMessage = "First name is required")]
    public string FirstName;
    [Required(ErrorMessage = "Last name is required")]
    public string LastName;

    // how to handle this?
    public Email[] EmailAddresses;
}

Answers


This can be a really lengthy post how to accomplish this. But there are several blogs that discuss how this can be done:

http://blog.codeville.net/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

and

http://zahidadeel.blogspot.com/2011/05/master-detail-form-in-aspnet-mvc-3-i.html

HTH


Need Your Help

What is faster: web service or XML server output?

c# .net xml web-services

The Central Bank of Russia as any other central bank provides a service to get exchange rates between ruble a number of other world currencies.