Asp.net mvc 获取mvc中单选按钮的选定值
我已与以下成员创建了DTO:Asp.net mvc 获取mvc中单选按钮的选定值,asp.net-mvc,razor,Asp.net Mvc,Razor,我已与以下成员创建了DTO: public List<Guid> QuestionIds { get; set; } public List<Guid> AnswerIds { get; set; } public CompetitionDTO Competition { get; set; } 公共列表问题ID{get;set;} 公共列表应答ID{get;set;} 公开竞赛竞赛{get;set;} 我想显示一个包含多个答案的问题列表,供
public List<Guid> QuestionIds { get; set; }
public List<Guid> AnswerIds { get; set; }
public CompetitionDTO Competition { get; set; }
公共列表问题ID{get;set;}
公共列表应答ID{get;set;}
公开竞赛竞赛{get;set;}
我想显示一个包含多个答案的问题列表,供用户查看,并让他们选择他/她确定的任何问题的正确答案。CompetitionTo具有以下风格:
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; }
}
公开课竞赛
{
公共Guid Id{get;set;}
公共字符串标题{get;set;}
公共字符串说明{get;set;}
公共日期时间起始日期{get;set;}
公共日期时间结束日期{get;set;}
公共IList问题{get;set;}
}
问题是:
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; }
}
公开课堂提问
{
公共Guid Id{get;set;}
公共字符串标题{get;set;}
公共字符串类别{get;set;}
公共IList答案{get;set;}
}
公共课堂回答
{
公共Guid Id{get;set;}
公共整数顺序{get;set;}
公共字符串标题{get;set;}
}
现在,在razor view中,我写了以下内容:
@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 })
}
}
for(var i=0;i但当我将数据传递给post action时,它不起作用,我想用他们的问题获得所有选定的答案,我应该如何解决这个问题?谢谢您的foreach“答案”循环对于您的模型来说没有意义。由于您使用单选按钮列表获取答案,我假设每个问题只能有一个答案,因此应将class
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)
}
}
for(var i=0;iIEnumerable
,其中设置了ID
和AcceptedAnswer
属性(除非包含其他隐藏输入,否则所有其他属性都将为空)此问题已得到回答