Skip to content

Instantly share code, notes, and snippets.

@KevM KevM/a_viewmodel.cs
Created Jan 24, 2012

Embed
What would you like to do?
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
You can’t perform that action at this time.