2

I have the following models

public class Person
{
    public string Name { get; set; }
    public IList<Phone> PhoneNumbers { get; set; }

}

public class Phone
{
    public string Number { get; set; }
}

Suppose I have the following Action :

    public ActionResult Edit()
    {
        Person p = new Person
        {
            Name = "John K.",
            PhoneNumbers = new List<Phone>
            {
                new Phone {Number = "555-555-5555"},
                new Phone {Number = "555-123-4444"}
            }
        };

        return View(p);
    }

I would like the view to allow the user to add/delete/modify the phone numbers.

I searched a lot for a simple solution... I can't believe that kind of scenario which occurs often can't be handle easily...

Baral
  • 3,103
  • 2
  • 19
  • 28
  • What specific problem are you having? Is something throwing exceptions? If so, what is the exception? Right now, I don't see a single question mark in your whole post. If this is a request for a tutorial on how to allow someone to edit a collection of items, StackOverflow is not the place for such a question. – Becuzz Sep 08 '15 at 15:53
  • If `Phone` only contains one property, then you may as well change `public IList PhoneNumbers { get; set; }` to public IList PhoneNumbers { get; set; }` and delete the class. Then in the view all you need is to dynamically add `` and the collection will be correctly bound. –  Sep 08 '15 at 22:57
  • If you do need more properties of `Phone`, then refer the answers [here](http://stackoverflow.com/questions/29161481/post-a-form-array-without-successful/29161796#29161796) and [here](http://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) –  Sep 08 '15 at 22:57

2 Answers2

-2

You can use Visual studio scaffolding

  • 1
    How would scaffolding possibly help in dynamically adding new collection items in a form! –  Sep 12 '15 at 04:26
-2

Although not specific to ASP.NET MVC, your problem can be solved by using knockout.js . In case you haven't used it before, it's not that hard to pick up and basics would do for this functionality. Basically, what you'd do is parse your C# model into a knockout view model, where in you maintain the data. All actions like create, delete or modify would be made to this view model and this model would be sent to your POST action in controller.

Example : http://knockoutjs.com/examples/collections.html

Anudeep Rentala
  • 150
  • 1
  • 6