0

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.

Community
  • 1
  • 1
Michael Herold
  • 1,292
  • 1
  • 13
  • 22
  • Check out http://stackoverflow.com/questions/8357468/radiobuttonfor-not-selecting-value-in-editor-templates, I believe it's the same issue you're having. – DMulligan May 17 '12 at 22:11
  • From that answer, it looks like that technique is inappropriate for editor templates. Since I'm making an editor template, it seems that won't work for me. Does anyone have any thoughts on whether that's right or not? – Michael Herold May 18 '12 at 00:01
  • I think that question implies you can use an editor template for what you want, you just can't use RadioButtonFor in the editor template. – DMulligan May 18 '12 at 00:31
  • Ah, I must have read that wrong. Thanks for the info! – Michael Herold May 18 '12 at 00:36

1 Answers1

1

Based on RadioButtonFor not selecting value in editor templates

Your model binding is fine, the issue is just that

@Html.RadioButtonFor(model => model, questionType)

corresponds to an empty name, and RadioButtonFor never checks radio buttons with empty names.

You should be able to do

@Html.RadioButton("", questionType, questionType == Model)
Community
  • 1
  • 1
DMulligan
  • 8,993
  • 6
  • 33
  • 34
  • This worked well. It's much cleaner than the hackish way I discovered. I figured out a way to do it using a attribute dictionary. It was pretty ugly, but this is much nicer. Thanks! – Michael Herold May 18 '12 at 00:37