mercredi 24 juin 2015

Bind checkboxes to List in partial view

I have a CreateViewModel.

public class CreateViewModel
{
  public AttributesViewModel AttributesInfo { get; set; }
}

The AttributesViewModel is sent to a partial view.

public class AttributesViewModel
{
  public AttributesViewModel()
  {
    ChosenAttributes = new List<int>();
  }

  public List<Attributes> Attributes { get; set; }
  public List<int> ChosenAttributes { get; set; }
}

The List of Attributes is outputted in the partial view. Each one has a checkbox.

foreach (var attribute in Model.Attributes)
{
  <input type="checkbox" name="ChosenAttributes" value="@attribute.ID" /> @Attribute.Name
}

When I post CreateViewModel, AttributesInfo.ChosenAttributes is always empty even though I checked some boxes. How do I properly name each checkbox so that it binds to the ChosenAttributes List?




Aucun commentaire:

Enregistrer un commentaire