FormHelper.Select with Enum
If you’re using Castle Monorail, and trying to use the FormHelper to create a form select janger from an enumerated type, you may or may not be confused as I was. It’s not obvious, and I couldn’t find many good docs, outside of a forum post.
This is how to do it. First, you have to pass the Enum type into your view data (here: MemberRoles).
public void Edit([ARFetch("mid")] MembershipInformation membership) { PropertyBag["MemberRoles"] = typeof (MemberRole); PropertyBag["membership"] = membership; }
Then you have to wrange with FormHelper. I have a love-hate relationship with FormHelper. It can be really frustrating sometimes. Anyway, here’s the syntax for how to make your select statement.
$FormHelper.Select("membership.MemberRole", $FormHelper.EnumToPairs($MemberRoles), $DictHelper.Create("value=First", "text=Second"))
What FormHelper.EnumToPairs does is create an array of Pair<int, string> objects. The Pair<> object has 2 properties, First, and Second. First, in this case, is an integer with the enum value, Second is a string of the enum’s name. So you pass that bad boy into your FormHelper as the array of select thingies, and then tell it to use the "First" property as the value of the select and the "Second" as the text to display.
Magic! Painful, painful magic! Maybe I should write my own FormHelper.EnumSelect thing. Hrmm…
Post a Comment