2009-04-22 75 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... 
}