How do I build a custom html helper like Html.TextBoxFor()
I would like to create a date-picker helper.
How do I build a custom html helper like Html.TextBoxFor()
I would like to create a date-picker helper.
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));
}