16

I was wondering if it is possible to create a custom strongly typed HTML Helper in ASP.NET MVC 2? Creating a regular (read not-strongly-typed) helper is straightforward but i am having difficulty creating strongly typed versions. For example, I would like to create a DatePickerFor html helper...

Any guidance or snippets would be greatly appreciated, Thank you in advance! JP

JP.
  • 5,536
  • 7
  • 58
  • 100

3 Answers3

14

Ok, I figured it out (and it was pretty straightforward...). Posting one of my overloads in case anyone else runs into this question.

public static string DatePickerFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel, TProperty>> expression)
  where TModel : class
{
    var inputName = ExpressionHelper.GetExpressionText(expression);
    return htmlHelper.DatePicker(inputName);
}
JP.
  • 5,536
  • 7
  • 58
  • 100
  • 2
    @JP how do you get the value ? e.g. html.DatePicker(x => x.Date), how do you get x.Date value – Omu Jan 10 '11 at 08:29
4

I just tried out the following to create a strongly typed CKEditor helper and it seems to be working flawlessly. This assumes that you already have included jquery and the necessary ckeditor scripts in your project. It might be nice to look at also setting the ckeditor config too, but this satisfied my current needs.

    public static MvcHtmlString CkEditor(this HtmlHelper htmlHelper, string name, string value, object htmlAttributes)
    {
        var output = htmlHelper.TextArea(name, value, htmlAttributes).ToString();
        output += string.Format("<script type=\"text/javascript\">$(document).ready(function(){{ $('#{0}').ckeditor(); }});</script>", name);

        return MvcHtmlString.Create(output);
    }

    public static MvcHtmlString CkEditor(this HtmlHelper htmlHelper, string name, string value)
    {
        return htmlHelper.CkEditor(name, value, null);
    }

    public static MvcHtmlString CkEditorFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes) where TModel : class
    {
        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        return htmlHelper.CkEditor(metadata.PropertyName, metadata.Model as string, htmlAttributes);
    }

    public static MvcHtmlString CkEditorFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) where TModel : class
    {
        return htmlHelper.CkEditorFor(expression, null);
    }
1
public static string DatePickerFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,Expression<Func<TModel, TProperty>> expression)
  where TModel : class
{
    ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
    return htmlHelper.DatePicker(metadata.PropertyName);
}

I used ModelMetadata this will also work if you create a datetime template for datepicker.

coolguy97
  • 565
  • 1
  • 7
  • 12
  • Does this account for `html.DatePickerFor(m => m.User.Birthday)`? (Multiple levels of dereferencing?) It looks like the `.PropertyName` member would just return the most recent name, and shouldn't it use all of the levels? – Carl G Dec 20 '12 at 21:16