0

I want to write my own custom HTML helper that extends an existing helper. E.g. I want to create to extend @Html.EditorFor like so:

@Html.EditorFor(model => model.percent, new { data_a_sign="%", data_p_sign="s" })

Becomes:

@Html.PercentEditorFor(model => model.percent)

How would one go about writing that?

Something like this?

namespace AdminPortal.Helpers
{
    public static class HtmlHelpers
    {
        public static MvcHtmlString PercentEditorFor<TModel>(this HtmlHelper html, 
            Expression<Func<TModel>> expression)
        {
            // Some Magic?
        }
    }
}

Any pointers would be greatly appreciated.

4

1 回答 1

1

只需从您自己的助手返回现有的 EditorFor 方法即可:

public static MvcHtmlString PercentEditorFor<TModel>(this HtmlHelper html, 
            Expression<Func<TModel>> expression)
        {
            return html.EditorFor(...);
        }

将您自己修改的参数放入 EditorFor 方法中。不需要魔法:)

于 2013-07-19T07:32:02.217 回答