0

我有一个列表中包含多个复选框项目的表单。我有这个列表的编辑器。它们在屏幕上显示良好。但是提交时检查的项目没有在模型中提交......

楷模:

public class ContactUsersModel : BaseModel
{
     public IList<FilterCheckModel> PreviousVisitors { get; set; }
}

public class FilterCheckModel : BaseModel
{
    public string Name { get; set; }
    public string Value { get; set; }
    public bool IsChecked { get; set; }        
}

控制器:

IList<FilterCheckModel> prevVisitList = new List<FilterCheckModel>();
prevVisitList.Insert(0, new FilterCheckModel() { Name = "All previous visitors", Value = "0" });
prevVisitList.Insert(1, new FilterCheckModel() { Name = "Not visited in last month", Value = "1" });
prevVisitList.Insert(2, new FilterCheckModel() { Name = "Not visited for 1-3 months", Value = "2" });
prevVisitList.Insert(3, new FilterCheckModel() { Name = "Not visited for 3-6 months", Value = "3" });
prevVisitList.Insert(4, new FilterCheckModel() { Name = "Not visited for over 6 months", Value = "4" });
model.PreviousVisitors = prevVisitList;

html:

 <table>
       <tr>
         <th>
             Previous visitors:
         </th>
       </tr>
       @for (int i = 1; i < Model.PreviousVisitors.Count; i++)
       {
           @Html.EditorFor(model => model.PreviousVisitors[i], "FilterCheck", new { Index = i + 1})
       }
  </table>

编辑:

<tr>
    <td>
        @Model.Name
    </td>
    <td>
        @Html.CheckBoxFor(x => x.IsChecked)
        @Html.HiddenFor(x => x.Value)
    </td>
</tr>
4

1 回答 1

0

找到了!

原因是在我的循环中,我开始了 i = 1。由于某种原因,这导致了一个问题。索引需要从 0 开始...

  @for (int i = 1; i < Model.PreviousVisitors.Count; i++)

所以我把它改成了

  @for (int i = 0; i < Model.PreviousVisitors.Count; i++)

它奏效了!

于 2013-01-15T11:35:54.753 回答