0

我正在尝试在 MonoRail 中使用 Checkboxlist 来表示多对多表关系。有一个Special 表,SpecialTag 表,然后是一个SpecialTagging 表,它是Special 和SpecialTag 之间的多对多映射表。

这是特殊模型类的摘录:

[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

复选框列表正确呈现,但如果我选择一些然后单击保存,它不会将特殊/标签关联保存到 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> 

有人有想法么?

谢谢!贾斯汀

4

2 回答 2

0

复选框列表正确呈现

在我看来,您还可以渲染类似

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

这使它更简单(名称没有输出索引,它将通过控制器操作参数绑定正确解析为数组

此外,在您的示例中,所有具有相同值UCampus.Core.Models.SpecialTag的复选框可能不正确,您可能希望从标签中输出实际的主键标识符(不确定,您能否显示您所在的类绑定回表单处理操作?)

于 2010-06-21T09:02:06.923 回答
0

我可以通过指定 id 和 text 属性来让它工作......

 $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
于 2010-06-01T23:38:43.460 回答