2010-06-01 51 views
0

我正在嘗試使用MonoRail中的Checkboxlist來表示多對多的表關係。有一個Special table,SpecialTag表,然後是SpecialTagging表,它是Special和SpecialTag之間的多對多映射表。MonoRail CheckboxList?

下面是從特殊模型類的摘錄:

[HasAndBelongsToMany(typeof(SpecialTag), 
     Table = "SpecialTagging", ColumnKey = "SpecialId", ColumnRef = "SpecialTagId")] 
     public IList<SpecialTag> Tags { get; set; } 

然後在我的添加/編輯特別觀點:

$Form.LabelFor("special.Tags", "Tags")<br/> 
    #set($items = $FormHelper.CreateCheckboxList("special.Tags", $specialTags)) 
     #foreach($specialTag in $items) 
      $items.Item("$specialTag.Id") $Form.LabelFor("$specialTag.Id", $specialTag.Name) 
    #end 

的CheckBoxList的正確渲染,但如果我選擇一些,然後單擊保存,它不會將特殊/標記關聯保存到SpecialTagging表(傳遞給保存控制器操作的實體具有空的標記列表)。我注意到的一件事是複選框的名稱和值屬性很奇怪:

<label for="special_Tags">Tags</label><br> 
        <input id="3" name="special.Tags[0]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="3">Buy 1 Get 1 Free</label> 
      <input id="1" name="special.Tags[1]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="1">Free</label> 
      <input id="2" name="special.Tags[2]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="2">Half Price</label> 
      <input id="5" name="special.Tags[3]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="5">Live Music</label> 
      <input id="4" name="special.Tags[4]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="4">Outdoor Seating</label> 

任何人有任何想法?

謝謝! 賈斯汀

回答

0

的CheckBoxList的正確呈現

在我看來,你也可以渲染類似於

<input type="checkbox" name="special.Tags" value="1"/> 
<input type="checkbox" name="special.Tags" value="2"/> 

它使它更簡單(沒有索引的名稱輸出,它將被正確解析經由控制器動作參數的陣列結合

也,在樣品中,即具有相同值的UCampus.Core.Models.SpecialTag是所有複選框的事實可能是不正確的,你可能想從標籤輸出實際的主鍵標識符(不確定,你能顯示你正在綁定的表單處理操作?)

0

我能夠得到它通過指定ID的工作和文本屬性...

$Form.LabelFor("special.Tags", "Tags")<br/> 
    #set($items = $FormHelper.CreateCheckboxList("special.Tags", $specialTags, "%{value='Id', text='Name'}")) 
     #foreach($specialTag in $items) 
      $items.Item("$specialTag.Id") $Form.LabelFor("$specialTag.Id", $specialTag.Name) 
    #end