tags:

views:

157

answers:

2
      <% foreach (FoodMenu f in (IEnumerable)ViewData.Model)
      { 
         %>            

        <ul >
          <li><%= Html.Encode(f.ProductId) %> </li>     
    <li><%= Html.Encode(f.Name) %></li>
    <li><%= Html.Encode(f.Price) %> </li>
     <li><%= Html.CheckBox("Selected") %></p></li>      

       </ul>

      </div> 

      <% } %>

I want to add the selected item into database

+1  A: 

Encode the id of the product that is selected into the name of the check box.

<%= Html.CheckBox("Selected_" + f.ProductId) %>

The on the server side, iterate through the ValueProviderKeys and find selections and extract the product id from the ones that are selected.

foreach (var key in this.ValueProvider.Keys.Where( k => k.StartsWith( "Selected_" ) )
{
     // for a checkbox I think this is something like true,false if the
     // visible checkbox is checked.  There is an invisible checkbox
     // (set to false), that is checked by default so you get both when
     // the true checkbox is checked.
     bool selected = this.ValueProvider[key].AttemptedValue.Contains("true");
     int productID = int.Parse( key.Replace("Selected_",null) );

     ...store selected product id in db...
}
tvanfosson
A: 

Neatest way is to use model binding for your list of checkboxes, see post here:
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

GONeale