Your not far off, just change your HelperFactory
to take in TModel
so you can tie it to a generic HtmlHelper
and make CustomEditorFor
generic so it can accept any TProperty
i.e.
public class HelperFactory<TModel>
{
public HelperFactory(HtmlHelper<TModel> htmlHelper)
{
this.HtmlHelper = htmlHelper;
}
public HtmlHelper<TModel> HtmlHelper { get; private set; }
public virtual EditorBuilder<TModel, TProperty> CustomEditorFor<TProperty>(Expression<Func<TModel, TProperty>> expression, EditorOptions options, object htmlAttributes)
{
return new EditorBuilder<TModel, TProperty>(this.HtmlHelper,
expression,
options,
htmlAttributes);
}
}
Then all you need to do is update the extension method
public static class HtmlHelperExt
{
public static HelperFactory<TModel> Custom<TModel>(this HtmlHelper<TModel> helper)
{
return new HelperFactory<TModel>(helper);
}
}
So for a model like
public class MyModel
{
public int Id { get; set; }
...
}
The usage would look like
@model MyModel
@Html.Custom().CustomEditorFor(x => x.Id, EditorOptions.Option1, null)