2010-06-01 50 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