2014-07-13 5 views
0

나는 다음과 같은 멤버와 DTO를 만들었습니다MVC에서 라디오 버튼의 선택 값을 가져

public List<Guid> QuestionIds { get; set; } 
    public List<Guid> AnswerIds { get; set; } 
    public CompetitionDTO Competition { 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; } 
    } 

:

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

을하지만 데이터를 전달할 때 작동하지 않습니다

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 : CompetitionDTO는 다음과 같은 스타일을 가지고 행동을 게시하려면, 내가 선택한 모든 답변을 자신의 질문과 함께 갖고 싶습니다. 어떻게 해결해야합니까? 감사합니다

+1

이 이미 응답되었습니다. http://stackoverflow.com/questions/19964553/mvc-form-not-able-to-post-list-of-objects –

답변

1

'Answers'에 대한 foreach 루프는 모델과 관련하여 의미가 없습니다. 당신이 답변을 라디오 버튼리스트를 사용하고 있기 때문에, 난 단지 때문에 클래스 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) 
    } 
} 

다시 게시 할 때,이 당신에게 IDAcceptedAnswer 속성을 설정 IEnumerable<QuestionDTO>를 제공해야합니다 (추가 숨겨진 입력을 incude하지 않는 다른 모든 속성이 null이됩니다)

관련 문제