I am trying to provide a radio button list to choose an option from a small set of strings within an MVC 3 model. I am working on is the editor template for question within a survey.
So far I have followed Brad Christie's advice, which has allowed me to render the view properly. Here's the relevant code:
Model - Question.cs
(Note, this is actually a model-first EF entity, but I don't believe it matters)
public class Question {
// snip unimportant properties
public string QuestionType { get; set; }
}
View - Question.cshtml
// snip unimportant view code
<div class="control-group">
@Html.LabelFor(model => model.QuestionType, "Question Type")
<div class="controls">
@Html.EditorFor(model => model.QuestionType, "QuestionType")
@Html.ValidationMessageFor(model => model.QuestionType)
</div>
</div>
View - QuestionType.cshtml
@model System.String
@{
var questionTypes = new String[] { "PickOne", "PickMany", "OpenEnded" };
var typesMap = new Dictionary<String, String> {
{ "PickOne", "Pick a single answer" },
{ "PickMany", "Pick several answers" },
{ "OpenEnded", "Open-ended answer" }
};
}
@foreach (var questionType in questionTypes) {
<label class="radio">
@Html.RadioButtonFor(model => model, questionType)
@typesMap[questionType]
</label>
}
Though the view creates the correct HTML, when I load up the editor view for a survey, the radio button corresponding to its question type is not pre-selected upon page load. I'm assuming this has to do with model binding?
How can I pre-select the correct radio button based on the choice that exists in the database? In case it's important, I am using model-first Entity Framework 4 as my persistence layer.