@Html.DatePickerFor with bootstrap datepicker using TagBuilder

4.3k views Asked by At

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?

1

There are 1 answers

1
Robert On BEST ANSWER

For those of you trying to write your own @Html.DatePickerFor() Here is what I did to get it working.

 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 spanWrapper = new TagBuilder("span");
        spanWrapper.AddCssClass("add-on");

        var iconThWrapper = new TagBuilder("i");
        iconThWrapper.AddCssClass("icon-th");

        GetPropValue(htmlHelper.ViewData.Model, propertyName);
        var inputTag = htmlHelper.EditorFor(expression).ToHtmlString();


        spanWrapper.InnerHtml += iconThWrapper.ToString(TagRenderMode.Normal);


        datePicker.ToString(TagRenderMode.Normal);
        datePicker.InnerHtml += inputTag + spanWrapper.ToString(TagRenderMode.Normal); ; 

        controlWrapper.InnerHtml += datePicker.ToString(TagRenderMode.Normal);

        controlGroupWrapper.InnerHtml += controlWrapper.ToString(TagRenderMode.Normal);



        return MvcHtmlString.Create(controlGroupWrapper.ToString(TagRenderMode.Normal));

    }