使用TagBuilder使用bootstrap datepicker创建@Html.DatePickerFor

时间:2013-08-05 19:43:11

标签: asp.net-mvc-4 twitter-bootstrap tagbuilder

所以我试图为bootstrap日期选择器添加一个扩展名:

 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));

    }

这就是我拥有和我需要的是:

 <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>

我实际在屏幕上看到的是:

<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>

正如你所看到的那些东西没有排队,我在屏幕上得到了实际的datepicker div,而不是通过JS插入。任何人对我做错了什么有任何想法?

1 个答案:

答案 0 :(得分:1)

对于那些试图编写自己的人来说@ Html.DatePickerFor()以下是我为使其工作而采取的措施。

 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));

    }