Created
September 10, 2012 18:38
-
-
Save KevM/3692811 to your computer and use it in GitHub Desktop.
FubuMVC html convention element builder to create select elements from enum properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class EnumDropdownBuilder : ElementBuilder | |
| { | |
| protected override bool matches(AccessorDef def) | |
| { | |
| var type = def.Accessor.PropertyType; | |
| return type.IsEnum; | |
| } | |
| public override HtmlTag Build(ElementRequest request) | |
| { | |
| return new SelectTag(tag => | |
| { | |
| buildOptions(request, tag); | |
| tag.AddClass("enum-list"); | |
| }); | |
| } | |
| private static void buildOptions(ElementRequest request, SelectTag tag) | |
| { | |
| var enumType = request.Accessor.PropertyType; | |
| foreach (var name in Enum.GetNames(enumType)) | |
| { | |
| tag.Option(name, name); | |
| } | |
| var requestValue = request.StringValue(); | |
| var defaultValue = requestValue.IsNotEmpty() ? requestValue : getDefaultValue(request, enumType); | |
| tag.SelectByValue(defaultValue); | |
| } | |
| private static string getDefaultValue(ElementRequest request, Type enumType) | |
| { | |
| var propertyDefaultValueFromAttribute = ""; | |
| if (request.Accessor.HasAttribute<DefaultValueAttribute>()) | |
| { | |
| var defaultValueFromAttribute = request.Accessor.GetAttribute<DefaultValueAttribute>().Value.ToString(); | |
| if (Enum.GetNames(enumType).Any(n => n == defaultValueFromAttribute)) | |
| propertyDefaultValueFromAttribute = defaultValueFromAttribute; | |
| //else log a nice trace warning but I'd rather this type of thing be a compiler or lint type warning?? | |
| } | |
| return propertyDefaultValueFromAttribute; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// Import this in your ConfigureFubuMVC.cs file via: | |
| // | |
| /// Import<MyHtmlConventions>(); | |
| // | |
| public class MyHtmlConventions : HtmlConventionRegistry | |
| { | |
| public static readonly string EDIT_PROFILE = "edit"; | |
| public MyHtmlConventions() | |
| { | |
| setUpFormConventions(); | |
| } | |
| private void setUpFormConventions() | |
| { | |
| Editors.Builder<EnumDropdownBuilder>(); | |
| Editors | |
| .If(prop => prop.Accessor.FieldName.ToLower().Contains("password")) | |
| .Modify(tag => tag.Attr("type", "password")); | |
| Editors.If(prop => prop.Accessor.FieldName.Equals("Text")) | |
| .Modify(tag => tag.TagName("textarea")); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MyModel | |
| { | |
| [DefaultValue("Customer")] | |
| public SiteType Type { get; set; } | |
| } | |
| public enum SiteType | |
| { | |
| Customer, | |
| Internal, | |
| Reseller, | |
| Individual, | |
| Vendor, | |
| } |
Author
Dru mentioned that it could accept a enum value as well. Throwing together an update and some tests now.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
coolio