2010-03-04 71 views
8

如何將MultiSelectList與複選框列表關聯?ASP.NET MVC:從MultiSelectList呈現複選框列表

例如。我把這樣的東西傳給模型

model.Groups = new MultiSelectList(k.Groups, "Id", "Name", selectedGroups) 

我該如何渲染它?這不起作用

<% foreach (var item in Model.Groups.Items) { %> 
    <input type="checkbox" name="groups" value="<%=item.Value%>" id="group<%=item.Value%>" checked="<%=item.Selected?"yes":"no"%>" /> 
    <label for="group<%=item.Value%>"><%=item.Text%></label> 
<% } %> 

錯誤CS1061:「對象」不包含「價值」的定義......

有沒有我可以使用HTML輔助方法?

(然後,除非它是簡單的,我應該怎麼然後得到所選擇的值回控制器時提交表單?)

+0

需要如何在控制器的組?如果您剛剛從表單中收到組ID,這足夠了嗎? – 2010-03-04 06:24:12

+0

我只需要知道選擇是否已經改變(即提交表單時檢查哪些選項)。 但更重要的是如何呈現的複選框 – Aximili 2010-03-04 06:30:41

回答

18

我只是測試,看看我們如何可以看到,如果選擇發生了變化。

public class Group { 
    public int ID { get; set; } 
    public string Name { get; set; } 
} 

//And some data to play with 
var allGroups = new List<Group>(); 
allGroups.Add(new Group { ID = 1, Name = "one" }); 
allGroups.Add(new Group { ID = 2, Name = "two" }); 
allGroups.Add(new Group { ID = 3, Name = "three" }); 

var selectedGroups = new List<Group>(); 
selectedGroups.Add(allGroups[0]); 
selectedGroups.Add(allGroups[2]); 

var m = new MultiSelectList(allGroups, "ID", "Name", 
    selectedGroups.Select(x => x.ID)); 

//passed that data to the view with ViewData 
ViewData["list"] = m; 

複選框元素:

<% foreach (var item in (MultiSelectList)ViewData["list"]) { %> 
    <input type="checkbox" name="groups" value="<%=item.Value%>" 
     id="group<%=item.Value%>" 
     <%=item.Selected ? "checked=\"checked\"" : String.Empty%>/> 
    <label for="group<%=item.Value%>"><%=item.Text%></label> 
<% } %> 

接受一個int數組中的作用:

[HttpPost] 
public ActionResult SomeAction(int[] groups) { 
    if (groups != null) { 
     var postedSelection = allGroups.Where(x => groups.Contains(x.ID)); 
     if (!selectedGroups.SequenceEqual(postedSelection)) { 
      //selection was changed 
     } 
     else { 
      //selection is the same 
     } 
    } 
    else { 
     //no group ID was posted 
    } 
} 

我希望給出了一些想法。

+0

我看到的,所以它應該是 的foreach(在Model.Groups VAR項目) 代替 的foreach(在Model.Groups.Items VAR項目) 謝謝cagdas ! – Aximili 2010-03-04 23:43:50

2

我只是想用上面的例子來分享我的實現。我正在填充組織到數據庫中的鍵/值對中的屬性。在我的示例中,我將每個屬性都作爲一個字典中的鍵值對存儲。字典中的每個項目將包含一個字符串鍵,如「顏色」,並像DataValueField =「1」,DataTextField =「黑」等等

VewModel代碼

public Dictionary<string, MultiSelectList> Properties { get; private set; } 

    private void SetProperties() 
    { 
     this.Properties = new Dictionary<string, MultiSelectList>(); 

     foreach(InventoryItemProperty property in new InventoryItemPropertyRepository().FindAllInventoryItemProperties()) 
     { 
      this.Properties.Add(property.Key.Name, new MultiSelectList(property.Values, "Id", "Value")); 
     } 
    } 

查看值MultiSelectList標記

<div id="editor-inventory-item-properties"> 
     <% foreach(string key in Model.Properties.Keys){ %> 
      <div class="editor-label"> 
       <label for="<%= key.ToLower() %>"><%= key %></label><br /> 
       <% foreach(var item in Model.Properties[key]){ %> 
        <input type="checkbox" 
         id="<%= key.ToLower() + "-" + item.Text.ToLower() %>" 
         name="inventoryItemPropertyValues" 
         value="<%= item.Value %>" 
         <%= item.Selected ? "checked=\"checked\"" : string.Empty %> /> 
        <label for="<%= key.ToLower() + "-" + item.Text.ToLower() %>"> 
        <%= item.Text %></label><br /> 
       <% } %> 
      </div> 
     <% } %> 
    </div> 

控制器動作代碼

// 
    // POST: /Admin/InventoryItems/Create 

    [HttpPost] 
    public ActionResult Create(InventoryItem inventoryItem, int[] inventoryItemPropertyValues) 
    { 
     try 
     { 
      inventoryItem.Created = DateTime.Now; 
      inventoryItem.LastUpdated = inventoryItem.Created; 
      this.inventoryItemRepository.Add(inventoryItem); 
      this.inventoryItemRepository.Save(); 

      if(inventoryItemPropertyValues != null) 
      { 
       SaveInventoryItemPropertyValues(inventoryItem.Id, inventoryItemPropertyValues); 
      } 

      return RedirectToAction("Details", new { id = inventoryItem.Id }); 
     } 
     catch 
     { 
      throw; 
      //return View(); 
     } 
    } 
private void SaveInventoryItemPropertyValues(int inventoryItemId, int[] inventoryItemPropertyValues) 
    { 
     for(int i = 0; i < inventoryItemPropertyValues.Length; i++) 
     { 
      this.inventory_Item_ProperytValueRepository.Add(new Inventory_Item_PropertyValue() { InventoryItemId = inventoryItemId, InventoryItemPropertyValueId = inventoryItemPropertyValues[i] }); 
     } 

     this.inventory_Item_ProperytValueRepository.Save(); 
    }