How can I create a list of checkboxes that tie back to my model? I'm not understanding how I can get the list of checkboxes back when I post to the controller.
I have a view model:
public class Widget
{
public int ID { get; set; }
public string WidgetName { get; set; }
public string WidgetDescription { get; set; }
public List<MyPart> Parts { get; set; }
}
public class MyPart
{
public int ID { get; set; }
public string PartName { get; set; }
public string PartDescription { get; set; }
}
I am displaying the Widget (ID, Name, Description) in my cshtml page.
I created a partial page to display the list of parts.
@model IEnumerable<MySite.DataAccess.MyPart>
@foreach (var itm in Model)
{
<li style="list-style-type:none">
@(Html.Kendo().CheckBox().Name("parts").Label(@itm.PartDescription).HtmlAttributes(new { @id = @itm.ID.ToString() }))
</li>
}
Called in my main cshtml as:
@Html.Partial("_MyPartial", @Model.Parts)
EDIT: I have also tried:
@model IEnumerable<MySite.DataAccess.MyPart>
@foreach (var itm in Model)
{
<li style="list-style-type:none">
@(Html.CheckBoxFor(x => @itm.IsSelected, new { id = @itm.ID.ToString() }))
@(Html.HiddenFor(x => @itm.ID))
@(Html.DisplayFor(x => @itm.PartDescription ))
</li>
}
Of course adding a IsSelected property to my MyPart class.
On the post I'm still not seeing the Parts collection (Parts = null)