views:

131

answers:

1

This question has been asked many times but none that I really understand. I have a checkbox list in a generated table. I want to submit the form and retrieve the checkbox values and capture the checked boxes.

In the view: <% foreach (var item in Model.Results) { %> <%= Html.CheckBox("selectedItem", new { value = item.ItemId })%> <% } %>

In the viewmodel, how should I declare "selectedItem"? as a Inumerable, Int[], ....

In the controller I want the viewmodel to contain the list of objects that are selected,so I can submit to the database.

Thanks, Dean

A: 

You can use an IEnumerable of most any type to bind to the view.

Getting the selected values back is more difficult since the post will return the selected ItemIds in a comma seperated list. Your best bet is to use a custom model binder. Here's a great example

http://stackoverflow.com/questions/2343913/asp-net-mvc2-custom-model-binder-examples

The best bet is to use the default model binder to bind up what it can, then split the returned ItemIds and add these to the results list.

Cheddar