views:

90

answers:

1

How do I build a custom html helper like Html.TextBoxFor()

I would like to create a date-picker helper.

+2  A: 

I found these on a search. They appear to be very promising, and are in a clean tutorial-style format.

Edit: I downloaded the sample project from the first tutorial, and added the following to the HtmlHelperExtensions class to get strongly-typed ...For() behavior. The principle is simple: forward strongly typed data binding to the existing plain methods. Note that this has the Works on My Machine seal of approval, and should be carefully reviewed and, if necessary, adapted to your scenario.

public static string DatePickerFor<TModel, TProperty>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, TProperty>> expression,
    string imageUrl)
{
    if (expression == null)
    {
        throw new ArgumentNullException("expression");
    }

    var expressionText = ExpressionHelper.GetExpressionText(expression);
    return DatePicker(htmlHelper, expressionText, imageUrl, htmlHelper.ViewData.Eval(expressionText));
}
kbrimington
Nice tutorial on how to build a custom DatePicker Helper. My only concern is that there is nothing in theses tutorial about Model extension like Html.TextBoxFor(model => model.MyDate)
Jean-Francois
@Jean - I added a little something that might help bridge the gap between the tutorials and the `DatePickerFor` behavior you want. Feel free to check it out.
kbrimington
Thanks kbrimington, it's exactly what I was looking for.
Jean-Francois