0

所以我想为引导日期选择器添加一个扩展:

 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 插入。有人对我做错了什么有任何想法吗?

4

1 回答 1

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

    }
于 2013-08-06T12:46:48.170 回答