質問

I have the following html helper method:

public static MvcHtmlString CustomEditorFor<TModel, TProperty>(this HtmlHelper<TModel> helper,
        Expression<Func<TModel, TProperty>> expression, 
        EditorOptions options,
        object htmlAttributes)
{
}

I have a need to put move it to into a helper library that is structured in this way:

public class HtmlHelperExenion
{
    public static Custom(this HtmlHelper helper)
    {
        return new HelperFactory(helper);
    }
}

public class HelperFactory
{
    internal HtmlHelper HtmlHelper { get; set; }
    public HelperFactory(HtmlHelper htmlHelper)
    {
        this.HtmlHelper = htmlHelper;
    }

    public virtual EditorBuilder CustomEditorFor( parameters from static MvcHtmlString static method above go here)
    {
        return new EditorBuilder( parameters go here );
    }
}

I am unable to figure out how to structure my HelperFactory class and the EditorBuilder class constructor to handle the generics correctly.

This is what I tried and it didn't work:

public static HelperFactory<TModel, TProperty> Custom<TModel, TProperty>(this HtmlHelper<TModel> helper)
{
    return new HelperFactory<TModel, TProperty>(helper);
}

public class HelperFactory<TModel, TProperty> : HelperFactory
{
    public HtmlHelper<TModel> HtmlHelper { get; set; }

    public HelperFactory(HtmlHelper<TModel> htmlHelper)
        : base((HtmlHelper)htmlHelper)
    {
        this.HtmlHelper = htmlHelper;
    }

    public virtual EditorBuilder<TModel, TProperty> CustomEditorFor(Expression<Func<TModel, TProperty>> expression,
EditorOptions options,
object htmlAttributes)
    {
        return new EditorBuilder<TModel, TProperty>(this.HtmlHelper,
                   expression,
                   options,
                   htmlAttributes);
    }
}

public class EditorBuilder<TModel, TProperty>
{
    public EditorBuilder(HtmlHelper<TModel> helper,
        Expression<Func<TModel, TProperty>> expression,
        EditorOptions options,
        object htmlAttributes)
    {

    }

}

The end goal is to be able to use it in this manner: @Html.Custom().CustomEditorFor(model=>model.Property)

Any assistance would be greatly appreciated.

役に立ちましたか?

解決

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)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top