我正在尝试使用MonoRail中的Checkboxlist来表示多对多的表关系。有一个Special表,SpecialTag表,然后是一个SpecialTagging表,它是Special和SpecialTag之间的多对多映射表。
以下是Special model类的摘录:
[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)
#endcheckboxlist正确地呈现,但是如果我选择了一些,然后单击Save,它不会将特殊/标记关联保存到SpecialTagging表中(传递给save控制器操作的实体有一个空的标记列表)。我注意到的一件事是,复选框上的name和value属性很时髦:
<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> 有谁有什么想法吗?
谢谢!贾斯汀
发布于 2010-06-21 17:02:07
正确呈现检查框列表
在我看来,您还可以呈现如下内容
<input type="checkbox" name="special.Tags" value="1"/>
<input type="checkbox" name="special.Tags" value="2"/>这使得它变得更简单(没有输出名称的索引,它将通过控制器操作参数绑定正确地解析为数组
此外,在您的示例中,所有具有相同值UCampus.Core.Models.SpecialTag的复选框可能都不正确,因此您可能希望从标记中输出实际的主键标识符(不确定,您是否可以在表单处理操作上显示绑定回的类?)
发布于 2010-06-02 07:38:43
我可以通过指定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)
#endhttps://stackoverflow.com/questions/2954054
复制相似问题