我刚刚把自己弄得一团糟,我很难把自己想出来。我有以下域模型(为简洁而减少):
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; }
}
现在,当我Questionnaire
在我的视图中渲染我的时候,我对每个and使用EditorTemplates。在渲染我时,我正在查看属性(类似于 RadioButtonList 或 TextArea)和每个属性(这是一个逗号分隔的字符串)。例如,假设我们有一个这样的初始化:QuestionGroup
Question
Question
Type
Heading
Question
var question = new Question() {
Text = "My Question Text",
Type = "RadioButtonList",
Headings = "Very Difficult,2,3,4,Very Easy"
};
然后我们会得到这个:
这是在我的EditorTemplate中生成的,如下所示:
@foreach (var heading in Model.Headings.Split(','))
{
<li>
<div>
<strong>@heading</strong>
@Html.RadioButton(Model.Id.ToString(), heading)
</div>
</li>
}
此标记如下所示:
<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>
我已经制作了一个自定义模型活页夹,但这是我有点卡住的地方。我的实际问题是:
- 如何根据我的域模型在视图中保留选定的值?
- 我什至使用正确的方法来处理这样的事情,还是我离题了?
我必须承认,我还处于 MVC 的学习阶段,所以我自己的尝试可能有点盲目。任何帮助总是很感激!