I realized that in MVC4 use validation attributes to perform this task. But know I'm dealing with a different situation.
Here is the model, in it I have a collection property Choices
(this all the time has 5 elements) and CorectChoice
where is the selected index from the collection.
public class MultipleChoiceQuestionTemplate : QuestionTemplate
{
public MultipleChoiceQuestionTemplate() { ... }
[DisplayName("Question")]
public string QuestionText { get; set; }
public List<string> Choices { get; set; }
[DisplayName("Correct Choice")]
public int CorrectChoice { get; set; }
}
And here is the view, watch the presentation.
<div id="choices">
@for (int i = 0; i < Model.Choices5.Count; i++) {
<div class="choice-container" style="display: block;">
@Html.TextBoxFor(model => model.Choices5[i])
@Html.RadioButtonFor(model => model.CorrectChoice, i)
</div>
}
</div>
Note that the user can leave empty strings in the inputs (type="text"). I need to validate that the selected index (radiobuttons) should have a string.
How can I validate it before submit to the form?