我已经创建了一个包含以下成员的DTO:
public List<Guid> QuestionIds { get; set; }
public List<Guid> AnswerIds { get; set; }
public CompetitionDTO Competition { get; set; }
我想显示一个问题列表,其中包含几个为用户显示的答案,让他们选择他/她确定的任何问题的正确答案。 CompetitionDTO具有以下风格:
public class CompetitionDTO
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public IList<QuestionDTO> Questions { get; set; }
}
和QuestionDTO:
public class QuestionDTO
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Category { get; set; }
public IList<AnswerDTO> Answers { get; set; }
}
public class AnswerDTO
{
public Guid Id { get; set; }
public int Order { get; set; }
public string Title { get; set; }
}
现在在剃须刀视图中我写了这个:
@for (var i = 0; i < Model.Competition.Questions.Count; i++)
{
@Html.DisplayTextFor(x => x.Competition.Questions[i].Title)
foreach (var t in Model.Competition.Questions[i].Answers)
{
@Html.DisplayFor(c => t.Title)
@Html.RadioButtonFor(x => x.Competition.Questions[i].Answers, false, new { Model = t.Id })
}
}
但是当我将数据传递给后期操作时它不起作用,我想用他们的问题得到所有选定的答案,我该如何解决这个问题?感谢
答案 0 :(得分:1)
你的回答&#39;答案&#39;对你的模型没有意义。由于您使用单选按钮列表来获取答案,因此我假设每个问题只能有一个答案,因此应更改类class QuestionDTO
以包含已接受答案的属性
public class QuestionDTO
{
...
public Guid AcceptedAnswer { get; set; }
}
然后在视图中
@for (var i = 0; i < Model.Competition.Questions.Count; i++)
{
@Html.DisplayTextFor(x => x.Competition.Questions[i].Title)
// Add a hidden input for ID property assuming you want this to post back
@Html.HiddenFor(x => x.Competition.Questions[i].ID)
foreach (var t in Model.Competition.Questions[i].Answers)
{
@Html.DisplayFor(c => t.Title)
@Html.RadioButtonFor(x => x.Competition.Questions[i].AcceptedAnswer, t.ID)
}
}
回发后,这应该会为IEnumerable<QuestionDTO>
提供ID
和AcceptedAnswer
属性设置(除非您提供额外的隐藏输入,否则所有其他属性都将为空)