kamilws
kamilws

Reputation: 171

how to create HTML Helper to extend TextBoxFor?

Hi I've got following line of code:

@Html.TextBoxFor(m => m.StartDate,"{0:MM/dd/yy hh:mm:ss}",new{@class="form-control axDateTimePicker"})

How to write helper to look like

@Html.DateTimePickerHelper(m=>m.StartDate)

where the format string and classes are inside this new helper?

Upvotes: 0

Views: 1003

Answers (1)

shu
shu

Reputation: 1956

For this you need to create custom html helpers by using extension method Try the below code

namespace System.Web.Mvc
{

public static class CustomHtmlHelpers
{

    public static MvcHtmlString DateTimePickerHelper<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
    {
        var attributes = new RouteValueDictionary(htmlAttributes);
        string format = "{0:MM/dd/yy hh:mm:ss}";
        return System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper, expression, format, attributes);
    }

}
}

Upvotes: 5

Related Questions