I am working on an ASP.NET MVC application which allows users to create their own surveys. For the sake of simplicity in answering this question, lets say that they can only create surveys with multiple-choice questions, each of which will be displayed to a survey-taker as a question followed by a list of radiobuttons representing each possible answer. Users can create as many multiple-choice questions in a survey as they want.
So, I have a ViewModel as follows:
public class SurveyViewModel
{
public int Id { get; set; }
public IList<Question> Questions { get; set; }
}
public class Question
{
public int Id { get; set; }
public string Question { get; set; }
public IList<Answer> Answers { get; set; }
}
public class Answer
{
public int Id { get; set; }
public string LabelText { get; set; }
}
Usually, I would just add a field for each question in the form to the SurveyViewModel. However, in this case I obviously don't know how many questions exist because the user can create however many they want - so I can't create a field to store the user's answer for each question.
So my question is: How can I write the ViewModel and the View (in Razor syntax) so that I can submit a form to a Controller, such that the responses to each of the arbitrary number of multiple-choice questions can be saved?
I have spent a long time banging my head against the wall on this one, so all help is much appreciated! Thank you!