2009-04-22 74 views
0
 <% 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> 

     <% } %> 

選中的項目加入到數據庫中,我要選擇的項目添加到數據庫中如何使用ASP.NET MVC

回答

1

編碼選入複選框的名稱產品的ID。

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

的服務器端,遍歷的ValueProviderKeys和發現的選擇和利用被選擇的那些提取產品ID。

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... 
}