Skip to content

Instantly share code, notes, and snippets.

@KevM
Created January 24, 2012 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KevM/1672372 to your computer and use it in GitHub Desktop.
Save KevM/1672372 to your computer and use it in GitHub Desktop.
Bootstrap FubuMVC HtmlConvention
public class CreateCaseModel : IApi
{
public bool UserIsAuthenticated { get; set; }
public string ContactFirstName { get; set; }
public string ContactLastName { get; set; }
public string ContactPhone { get; set; }
public string SiteId { get; set; }
public string Queue { get; set; }
public string Title { get; set; }
public string Description { get; set; }
[GbstListValue(ListNames.CaseType)]
public string CaseType { get; set; }
[GbstListValue(ListNames.CaseSeverity)]
public string IssueSeverity { get; set; }
[GbstListValue(ListNames.CasePriority)]
public string Priority { get; set; }
}
<!-- ... form ... -->
<!-- render drop downs via convention -->
<div class="clearfix">
<label for="CaseType">Type</label>
<div class="input">
${this.InputFor(m=>m.CaseType)}
</div>
</div>
<div class="clearfix">
<label for="Severity">Severity</label>
<div class="input">
${this.InputFor(m=>m.IssueSeverity)}
</div>
</div>
<div class="clearfix">
<label for="Priority">Priority</label>
<div class="input">
${this.InputFor(m=>m.Priority)}
</div>
</div>
//generate a select tag based on the annotated GbstList in the IListCache
//When the model has a value set. The correct list element is selected. Handy for creation and editing.
public class GbstListValueDropdownBuilder : ElementBuilder
{
protected override bool matches(AccessorDef def)
{
var hasAttribute = def.Accessor.HasAttribute<GbstListValueAttribute>();
return hasAttribute;
}
public override HtmlTag Build(ElementRequest request)
{
return new SelectTag(tag =>
{
buildOptions(request, tag);
tag.AddClass("form-list");
});
}
private static void buildOptions(ElementRequest request, SelectTag tag)
{
var listElements = GetListElementTitlesOrderedByRank(request);
listElements.ElementTitles.Each(title => tag.Option(title, title));
var requestValue = request.Value<string>();
var defaultValue = requestValue.IsNotEmpty() ? requestValue : listElements.DefaultElementTitle;
tag.SelectByValue(defaultValue);
}
private static ListElements GetListElementTitlesOrderedByRank(ElementRequest request)
{
var att = request.Accessor.GetAttribute<GbstListValueAttribute>();
var listName = att.GetListName();
var listCache = request.Get<IListCache>();
var gbstList = listCache.GetGbstList(listName);
var elementTitles = gbstList.ActiveElements.OrderBy(e => e.Rank).Select(element => element.Title);
return new ListElements { DefaultElementTitle = gbstList.DefaultElement.Title, ElementTitles = elementTitles };
}
private class ListElements
{
public string DefaultElementTitle { get; set; }
public IEnumerable<string> ElementTitles { get; set; }
}
}
public class ConfigureFubuMVC : FubuRegistry
{
public ConfigureFubuMVC()
{
//other config
HtmlConvention(d=>
{
d.Editors.Builder<GbstListValueDropdownBuilder>();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment