So I am trying to add an extension for the bootstrap date picker:
public static MvcHtmlString DatePickerFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression)
{
var propertyName = ExpressionHelper.GetExpressionText(expression);
var controlGroupWrapper = new TagBuilder("div");
controlGroupWrapper.AddCssClass("control-group");
controlGroupWrapper.InnerHtml += htmlHelper.LabelFor(expression, new Dictionary<string, object> { { "class", "control-label" } }).ToHtmlString();
var controlWrapper = new TagBuilder("div");
controlWrapper.AddCssClass("controls");
var datePicker = new TagBuilder("div");
datePicker.AddCssClass("input-append date datepicker");
datePicker.MergeAttribute("data-date-format", "dd/mm/yyy");
var addOnWrapper = new TagBuilder("span");
addOnWrapper.AddCssClass("add-on");
var iconThWrapper = new TagBuilder("i");
iconThWrapper.AddCssClass("icon-th");
addOnWrapper.InnerHtml += iconThWrapper.ToString(TagRenderMode.Normal);
var value = GetPropValue(htmlHelper.ViewData.Model, propertyName);
var innerHtml = htmlHelper.EditorFor(expression).ToHtmlString();
innerHtml += addOnWrapper.ToString(TagRenderMode.Normal);
var datepickerInnerHtml = datePicker.ToString(TagRenderMode.Normal);
datepickerInnerHtml += innerHtml;
controlWrapper.InnerHtml += datepickerInnerHtml;
controlGroupWrapper.InnerHtml += controlWrapper.ToString(TagRenderMode.Normal);
return MvcHtmlString.Create(controlGroupWrapper.ToString(TagRenderMode.Normal));
}
That is what i have and what I need is:
<div class="control-group">
<label class="control-label" for="Date">@Model.DisplayName</label>
<div class="controls">
<div class="input-append date datepicker" data-date-format="dd/mm/yyyy">
<input name="@Model.Name" value="@Model.Value" class="span2" type="text"/>
<span class="add-on"><i class="icon-th"></i></span>
</div>
</div>
</div>
What I actually get on screen is:
<div class="control-group">
<label class="control-label" for="StartDate_Value">Value</label>
<div class="controls">
<div class="input-append date datepicker" data-date-format="dd/mm/yyy">
<div class="datepicker datepicker-inline" style="display: block;">
<div class="datepicker-days" style="display: block;">
<table class=" table-condensed">
<thead>
div>
</div>
</div>
<input class="text-box single-line" id="StartDate_Value" name="StartDate.Value" type="text" value="">
<span class="add-on">
<i class="icon-th"></i>
</span>
</div>
As you can see the stuff is not lining up and I am getting the actual datepicker div on screen instead of inserted via JS. Anyone got any ideas on what I am doing wrong?
For those of you trying to write your own @Html.DatePickerFor() Here is what I did to get it working.