我意识到在 MVC4 中使用验证属性来执行此任务。但要知道我正在处理不同的情况。
这是模型,其中我有一个集合属性Choices
(这一直有 5 个元素),CorectChoice
集合中的选定索引在哪里。
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; }
}
这是视图,请观看演示。
<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>
请注意,用户可以在输入中留下空字符串(type="text")。我需要验证所选索引(单选按钮)是否应该有一个字符串。
在提交表单之前如何验证它?