【发布时间】:2016-11-06 08:35:52
【问题描述】:
我将一个视图模型传递给看起来像这样的视图
assessment
questions
[0]
QuestionText
QuestionID
[1]
QuestionText
QuestionID
Answers
[0]
TheAnswer
QuestionID
基本上我的观点有一个问题列表和可能已经选择的任何答案的列表。
我需要显示所有问题和任何选择的答案
问答的链接是QuestionID
这是我在循环所有问题的视图中的剃刀代码
@foreach (var question in Model.Assessment.Questions)
{
<li class="row">
<div class="span9">
@Html.Raw(question.QuestionText)
</div>
<div class="span3">
@Html.Raw(<--No idea what to put here-->)
</div>
</li>
}
如何检索问题的选定答案?
编辑
这是我的模型
public class Assessment
{
public int ID { get; set; }
public Status Status { get; set; }
public DateTime AssessmentDate { get; set; }
public int AreaID { get; set; }
public virtual Area Area { get; set; }
public virtual ICollection<Question> Questions { get; set; }
public virtual ICollection<Answer> Answers { get; set; }
}
public class Question
{
public int ID { get; set; }
public string QuestionText { get; set; }
public int DisplayNumber { get; set; }
public int DisplayOrder { get; set; }
public string QuestionHelp { get; set; }
public bool isActive { get; set; }
public int CategoryID { get; set; }
public int ProcessID { get; set; }
public virtual Category Category { get; set; }
public virtual Process Process { get; set; }
public virtual ICollection<Assessment> Assessment { get; set; }
public virtual ICollection<Answer> Answers { get; set; }
}
public class Answer
{
public int ID { get; set; }
public int Value { get; set; }
public int QuestionID { get; set; }
public virtual Question Question { get; set; }
public virtual ICollection<Assessment> Assessments { get; set; }
}
【问题讨论】:
-
您的模型不正确 - 您应该有一个包含
QuestionID、QuestionText和TheAnswer的模型集合 -
不完全是。我的模型可能设计得很糟糕,但我的想法是一个问题可能有许多领域的答案(这些领域只是我对用户的阶段)。例如,user1 可能有问题 1 的答案,而 user2 也可能有问题 1 的答案。这样的设计好吗?
-
否(至少不是您展示的方式)。不知道
Assessment和Area之间有什么关系,但是Answer需要属性ID、TheAnswer(是Value吗?)、QuestionID和AreaID(或UserID)。 -
我刚刚看到你previous question 并假设这仍然基于那个数据库结构,你已经拥有了你需要的所有数据——你只需要创建一个具有@987654337 属性的视图模型@ 和
AnswerText以及每个Question,您可以将数据投影到该视图模型中 -
Area可以有多个Assessments。是的,它基于该数据库结构。我不确定如何实现您建议的视图模型。我有一个AssessmentViewModel,其中包含Assessment。Assessment有一个导航属性Answers我想我可以用它来查找问题的正确答案
标签: asp.net-mvc