I’ve just got myself into a little mess and am having trouble thinking myself out of it. I have the following domain model (reduced for brevity):
public class Questionnaire
{
public int Id { get; set; }
public IList<QuestionGroup> QuestionGroups { get; set; }
}
public class QuestionGroup
{
public int Id { get; set; }
public string Name { get; set; }
public int Order { get; set; }
public IList<Question> Questions { get; set; }
}
public class Question
{
public int Id { get; set; }
public string Text { get; set; }
public string Type { get; set; }
public string Headings { get; set; }
public IList<Answer> Answers { get; set; }
}
public class Answer
{
public int Id { get; set; }
public string Text { get; set; }
}
Now, when I’m rendering my Questionnaire in my view I’m using EditorTemplates for each QuestionGroup and Question. When rendering my Question I’m looking at the Type property (which is something like RadioButtonList or TextArea) and for each Heading (which is a comma separated string). So for example, let’s say we have a Question initialised like so:
var question = new Question() {
Text = "My Question Text",
Type = "RadioButtonList",
Headings = "Very Difficult,2,3,4,Very Easy"
};
Then we would end up with this:

Which is produced in my EditorTemplate like so:
@foreach (var heading in Model.Headings.Split(','))
{
<li>
<div>
<strong>@heading</strong>
@Html.RadioButton(Model.Id.ToString(), heading)
</div>
</li>
}
The markup for this looks like this:
<ul>
<li>
<div>
<strong>Very Difficult</strong>
<input id="group_question_1" name="group.question.1" type="radio" value="Very Difficult" />
</div>
</li>
<li>
<div>
<strong>2</strong>
<input id="group_question_1" name="group.question.1" type="radio" value="2" />
</div>
</li>
<li>
<div>
<strong>3</strong>
<input id="group_question_1" name="group.question.1" type="radio" value="3" />
</div>
</li>
<li>
<div>
<strong>4</strong>
<input id="group_question_1" name="group.question.1" type="radio" value="4" />
</div>
</li>
<li>
<div>
<strong>Very Easy</strong>
<input id="group_question_1" name="group.question.1" type="radio" value="Very Easy" />
</div>
</li>
</ul>
I have produced a Custom Model Binder but this is where I’m a little stuck. My actual questions are:
- How do I persist the selected value based on my domain model in the view?
- Am I even using the correct approach for something like this, or am I way off?
I must admit, I’m still very much in the learning stage of MVC so I could be a bit blind-sided from my own attempt. Any help always appreciated!
Managed to solve this using a custom model binder as mentioned in another question I asked: https://stackoverflow.com/a/12318484/961328