Consider the following.
Models
public class DemographicsModel
{
public List<QuestionModel> Questions { get; set; }
}
//Does not work at all if this class is abstract.
public /*abstract*/ class QuestionModel
{
//...
}
public class ChooseOneQuestionModel : QuestionModel
{
//...
}
public class ChooseManyQuestionModel : QuestionModel
{
//...
}
public class RichTextQuestionModel : QuestionModel
{
//...
}
public class TextQuestionModel : QuestionModel
{
//...
}
Controller
[HttpPost]
public ActionResult Demographics(DemographicsModel model)
{
//...
}
My view would have a DemographicsModel
with numerous question of all the varying types as shown above. After the form is completed and POST
ed back to the server, the Questions
property of the Demographics model
is re-populated with the correct number of questions but they are all of type QuestionModel
instead of the concrete type.
How do I make this thing understand what type to instantiate?