0

Is it possible to be able to add multiple items to a list while in a view? Let's say that I have the following data contract below:

public class Question
{
    public int QuestionID { get; set; }
    public string Name { get; set; }
    public virtual List<AdditionalAnswer> AdditionalAnswers { get; set; }
}

public class AdditionalAnswer
{
    public int AdditionalAnswerID { get; set; }
    public AnswerTypeAnswerTypeEnum  AnswerType { get; set; }
}

If I was creating a new Question, how would I add multiple AdditionalAnswerswhile still on the Edit view without doing any post-backs?

Justin Adkins
  • 1,214
  • 2
  • 23
  • 41
  • You will need to use javascript to dynamically add new elements and ensure they are property named (with indexers) to allow then to be posted back. Have a look at [this answer](http://stackoverflow.com/questions/24026374/adding-another-pet-to-a-model-form/24027152#24027152) for a possible solution –  Sep 06 '14 at 02:00

1 Answers1

2

The default model binding should be able to do it. The trick is to dynamically add input tags that look something like:

<input name="AdditionalAnswer[0].AdditionalAnswerID"/>
<input name="AdditionalAnswer[0].AnswerType"/>
<input name="AdditionalAnswer[1].AdditionalAnswerID"/>
<input name="AdditionalAnswer[1].AnswerType"/>
beautifulcoder
  • 10,832
  • 3
  • 19
  • 29