views:

37

answers:

2

I'm trying to use a Checkboxlist in MonoRail to represent a many to many table relationship. There is a Special table, SpecialTag table, and then a SpecialTagging table which is the many to many mapping table between Special and SpecialTag.

Here is an excerpt from the Special model class:

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

And then in my add/edit special view:

$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

The checkboxlist renders correctly, but if I select some and then click Save, it doesn't save the special/tag associations to the SpecialTagging table (the entity passed to the Save controller action has an empty Tags list.) One thing I noticed was that the name and value attributes on the checkboxes are funky:

<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> 

Anyone have any ideas?

Thanks! Justin

A: 

I was able to get it working by specifying the id and text attributes...

 $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
Justin
A: 

The checkboxlist renders correctly

it seems to me that you could also render something like

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

which make it simpler (no index to output for the name, it will be properly resolved as an array via controller action parameter binding

also, in your sample, the fact that all checkboxes having the same value UCampus.Core.Models.SpecialTag is probably not right, you may want to output actual primary key identifier from the tags (not sure, could you display the class you are binding back on the form handling action?)

smoothdeveloper