dimanche 21 décembre 2014

Unable to set checkbox checked on MVC page load

In my MVC view, I have a dynamically created list of checkboxes.



@foreach (var door in Model.Doors)
{
<input type="checkbox" id="@door.ID" name="DoorIds" value="door.ID"/>@Html.Label(door.DoorName);
}


My model -



public virtual IList<int> assignedDoors { get; set; }
public int[] DoorIds { get; set; }


When I tick some checkboxes and press Save, the changes are committed to the database. However, I can't seem to work out how to check the required boxes on page load?


If I put the below code in the HTML checkbox line,



checked="@Model.assignedDoors.Contains(@door.ID)" }


the checkboxes are checked, but when I press Save, I get the following error -



The value 'door.ID,door.ID' is not valid for DoorIds.



Any ideas where I am going wrong?





Aucun commentaire:

Enregistrer un commentaire