2014-11-27 4 views
0

예/아니오 유형 질문 목록을 사용자에게 제시해야합니다. 여기 내 모델은 다음과 같습니다여러 라디오 버튼 그룹에 데이터 모델을 연결하는 방법은 무엇입니까?

내 면도기 코드에서
class QuestionModel { 
    public String ID {get; set;} 
    public String Title {get; set; } 
    public bool Value {get; set; } 
} 

class QuestionsModel { 
    List<QuestionModel> Questions {get; set} 
} 

, 나는 같은 것을 할 수 있습니다 그러나

@foreach(var item in Model.Questions) { 
     @Html.Label(item.Title); 
     @Html.RadioButton(item.ID, "Yes"); 
     @Html.RadioButton(item.ID, "No"); 
    } 

을, 나는 QuestionModel에서 값 필드 기반 채워 방법을 볼 수 없습니다 사용자가 선택한 항목. 도와주세요. 문안 인사.

답변

1

은 첫째로 당신은 for 루프 또는 다른 컨트롤 이름이 올바른되지 않습니다 컬렉션을 생성하는 EditorTemplate을 사용할 필요가 돌아 게시물에

@model QuestionsModel 
@(using Html.BeginForm()) 
{ 
    for (int i = 0; i < Model.Questions.Count; i++) 
    { 
    string yes = i + "Yes"; 
    string no = i + "No"; 
    @Html.HiddenFor(m => m.Questions[i].ID) 
    @Html.DisplayFor(m => m.Questions[i].Title) // assuming you don't want to edit this 
    @Html.RadioButtonFor(m => m.Questions[i].Value, true, new { id = @yes }) 
    <label for="@yes">Yes</label> 
    @Html.RadioButtonFor(m => m.Questions[i].Value, false, new { id = @no }) 
    <label for="@no">No</label> 
    } 
    <input type="submit" value="Save" /> 
} 
을 바인딩 할 수 없습니다
관련 문제