2014-06-09 6 views
0

IList를 Html.CheckBoxFor에 바인딩하려고합니다.IList를 CheckBoxFor에 바인딩하면 체크 된 값이 반환되지 않습니다. MVC

내가 처음 시도하고 조사했을 때 KeyValuePair는 개인적인 특성 때문에 작업을 수행하지 않을 것이므로 MyKeyValuePair를 만들었습니다. 그래서 지금 내 모델에서 내가 가진 :

@Html.CheckBoxFor(model => model.MyProperty.Value) 
        @foreach (var f in Model.Orientation) 
        { 
         @Html.CheckBoxFor(model => f.Value) 
        } 

문제점가 IList의 내부에 그 MyKeyValuePair 자신의 값을 업데이트하지 않습니다,하지만 MyProperty는 :

public GameCreation() 
    { 
     Orientation = new List<MyKeyValuePair>(); 
     foreach (var v in Enum.GetNames(typeof(DeveloperPortalMVCApp.Models.Orientation))) 
     { 
      Orientation.Add(new MyKeyValuePair { Name = v }); 
     } 
    } 

    public MyKeyValuePair MyProperty { get; set; } 
    public ObservableCollection<MyKeyValuePair> Orientation { get; set; } 

내도이다. 내가 뭘 놓치고 있니? 당신이 인덱스를 체크 박스를하지 않으면 사용

@Html.CheckBoxFor(model => model.MyProperty.Value) 
@for (var i=0; i < Model.Orientation.Count; i++) 
{ 
    @Html.CheckBoxFor(model => Model.Orientation[i].Value) 
} 

답변

1

는, 인덱서에 특별한주의를 지불, 당신은 충돌 이름과 또는 ID를 가진 체크 박스의 무리와 함께 종료됩니다. 모델 바인더가이 항목을 목록이 아닌 단일 항목으로 바인딩하려고 시도했을 수 있습니다.

위의 코드 샘플을 사용하는 경우, 당신은이 비슷한 얻을 것이다 :

<input type="checkbox" name="Orientation[0].Value" /> 
<input type="checkbox" name="Orientation[1].Value" /> 
<input type="checkbox" name="Orientation[2].Value" /> 

모델 바인더 목록으로 해석 할 수 있습니다. 당신이 CheckBoxFor에 인덱서를 사용하지 않는 경우에, 당신이 비슷한 얻을 것이다 :

<input type="checkbox" name="Orientation.Value" /> 
<input type="checkbox" name="Orientation.Value" /> 
<input type="checkbox" name="Orientation.Value" /> 

을 그리고 ModelBinder를이 밖으로 목록을 만들 수 없습니다.