Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...