Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

asp.net mvc - Get the selected values of radio button in mvc

I've created a DTO with following members:

    public List<Guid> QuestionIds { get; set; }
    public List<Guid> AnswerIds { get; set; } 
    public CompetitionDTO Competition { get; set; }

I wanna display a list of questions contained several answers to show for users and let them to choose the correct answers of any question that he/she is sure about. CompetitionDTO has the following style:

 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; }

        }

and the 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; }
    }

now in razor view I written this:

 @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 })
            }
         }    

but it doesn't work when I pass the data to post action, I want to get the all selected answers with theirs questions, How should I solve this? thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your foreach loop for 'Answers' does not make sense in relation to your model. Since you are using a radio button list for answers, I assume there can only be one answer for each question, therefore class class QuestionDTO should be changed to include a property for the accepted answer

public class QuestionDTO
{
  ...
  public Guid AcceptedAnswer { get; set; }
}

then in the view

@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)
  }
}

When posting back, this should give you IEnumerable<QuestionDTO> where the ID and AcceptedAnswer properties are set (all other properties will be null unless you incude additional hidden inputs)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...