formcollection only holds the selected html.listbox items values? MVC

前端 未结 3 593
刺人心
刺人心 2020-12-29 08:38

My scenario is this: I have two listbox\'s, one that contains all my database items, and an empty one. The user adds the items needed from the full listbox to the empty list

3条回答
  •  我在风中等你
    2020-12-29 09:28

    Why not have the list of items in checkboxes. Then you could iterate through the checkboxes in your action and grab all selected checkboxes.

    <% foreach(var item in Model.Items) { %>
    
       <%= Html.CheckBox(String.Format("ItemID.{0}", item.ID)) %> // each item tagged by the items id
    
    <% } %>
    
    public ActionResult MyAction(FormCollection formCollection)
    {
    
                foreach (var key in collection.AllKeys.Where(k => !k.Contains("SubmitButton")).ToArray())
                {
                     // iterates thru check boxes we got rid of the button 
    
                }
    }
    

提交回复
热议问题