【问题标题】:MonoRail CheckboxList?MonoRail 复选框列表?
【发布时间】:2010-06-01 23:28:58
【问题描述】:

我正在尝试在 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> 

有人有什么想法吗?

谢谢! 贾斯汀

【问题讨论】:

    标签: castle-monorail


    【解决方案1】:

    复选框列表正确呈现

    在我看来你也可以渲染类似的东西

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

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

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

    【讨论】:

      【解决方案2】:

      我能够通过指定 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-26
        • 1970-01-01
        相关资源
        最近更新 更多