0

I have a form with serval check box items in a list. I have a editor for this list. They display on the screen fine. But when submitted the items checked are not submitted in the model...

Models:

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; }        
}

Controller:

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>

editor:

<tr>
    <td>
        @Model.Name
    </td>
    <td>
        @Html.CheckBoxFor(x => x.IsChecked)
        @Html.HiddenFor(x => x.Value)
    </td>
</tr>
Beginner
  • 28,539
  • 63
  • 155
  • 235

1 Answers1

0

Found it!

the reason was that in my loop i was started i = 1. this causes an issue for some reason. The index need to start form 0...

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

so i changed this to

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

and it worked!

Beginner
  • 28,539
  • 63
  • 155
  • 235