我有一个创建和编辑调查的应用程序。每项调查都包含一组问题和相关的答案(答案)。创建调查时,问题集合将从单独的Questions
表生成。每年,都会为每个使用相同问题的用户创建一个新的调查,以便随时间对答案进行比较。
创建调查时,会保存每个问题的答案,但用户可能没有对每个问题做出回复,现在我必须构建一个视图来编辑现有的答案。
模型
public class Survey
{
public int ID { get; set; }
public int AreaID { get; set; }
public Status Status { get; set; }
public DateTime AssessmentDate { get; set; }
public virtual Area Area { get; set; }
public virtual ICollection<Answer> Answers { get; set; }
}
public class Question
{
public int ID { get; set; }
public string QuestionText { get; set; }
public virtual ICollection<Answer> Answers { get; set; }
}
public class Answer
{
public int ID { get; set; }
public int? Response { get; set; }
public int QuestionID { get; set; }
public int SurveyID { get; set; }
public virtual Question Question { get; set; }
public virtual Survey Survey{ get; set; }
}
这是我用于创建编辑屏幕视图的视图模型
public class SurveyResponseViewModel
{
public Assessment Assessment { get; set; }
public IEnumerable<Question> Questions { get; set; }
}
并且GET方法中的代码是
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Survey survey = db.Surveys.Find(id);
var viewModel = new SurveyResponseViewModel
{
Survey = survey,
Areas = new SelectList(db.Areas, "ID", "SubLevel").ToList(),
Questions = db.Questions.Where(q => q.isActive)
};
if (survey == null)
{
return HttpNotFound();
}
return View(viewModel);
}
这会在所有问题中填充我的视图模型,但每个问题都包含一系列答案。如何在视图中仅显示和编辑与此调查相关的每个问题的答案?
@foreach (var question in Model.Questions)
{
// Display the question
@Html.Raw(question.QuestionText)
// How to create an input for the associated response??
<input type="text" name="????" placeholder="Enter a number..." value="????" />
}
请注意,回复为int?
,其值可以介于0
和5
之间(如果用户尚未提供回复,则可以为null
)。理想情况下,我希望将其渲染为单选按钮以选择可能的值。
答案 0 :(得分:1)
首先,您需要创建一个表示问题及其答案的视图模型
public class QuestionVM
{
public int QuestionID { get; set; }
public string QuestionText { get; set; }
public int? AnswerID { get; set; }
[Range(0, 5)]
public int? Response { get; set; }
}
并且主视图模型将是(注意视图模型不应包含编辑时作为数据模型的属性)
public class SurveyResponseVM
{
public int ID { get; set; } // this will be bound by the route value
[Required(ErrorMessage = "Please select an area")]
[Display(Name = "Area")]
public int? SelectedArea { get; set; }
public IEnumerable<SelectListItem> AreaList { get; set; }
.... // other properties of assessment that you need for the view
public List<QuestionVM> Questions { get; set; }
}
以便在视图中使用
@model SurveyResponseVM
....
@using (Html.BeginForm())
{
@Html.DropDownListFor(m => m.SelectedArea, Model.AreaList)
....
for(int i = 0; i < Model.Questions.Count; i++)
{
<h3>@Model.Questions[i].QuestionText</h3>
@Html.HiddenFor(m => m.Questions[i].AnswerID)
for (int j = 0; j < 6; j++)
{
<label>
@Html.RadioButtonFor(m => m.Questions[i].Resonse, j, new { id = "" })
<span>@j</span>
</label>
}
}
<input type="submit" value="Save" />
}
将发回
public ActionResult Edit(SurveyResponseVM model)
要获取GET方法中的值,您的第二个查询(db.Questions.Where(q => q.isActive)
似乎没有必要,因为您已经拥有与Answer
相关联的Survey
集合(以及每个Answer
包含Question
属性。您的代码可以是
Survey survey = db.Surveys.Find(id);
if (survey == null) // check for null here
{
return HttpNotFound();
}
IEnumerable<Area> areas = db.Areas;
SurveyResponseVM model = new SurveyResponseVM()
{
ID = survey.ID,
SelectedArea = survey.AreaID,
.... // other properties of Survey as required for the view
AreaList = new SelectList(areas , "ID", "SubLevel"),
Questions = survey.Answers.Select(x => new QuestionVM()
{
QuestionText = x.Question.QuestionText,
AnswerID = x.ID,
Response = x.Response
})
};
return View(model);