在MVC5中将htmlAttributes全局添加到EditorFor

时间:2015-09-25 10:20:37

标签: asp.net asp.net-mvc razor

我想在表单中禁用HTML ID,因为我在一个页面上使用多个Ajax表单。 我现在这样做:

@Html.EditorFor(model => model.AccountNumber, new { htmlAttributes = new { @class = "form-control", id="" } })

但我觉得必须有一种方法可以全局声明它,就像在自定义视图引擎配置中一样。我不喜欢在每个编辑器上执行此操作,并且我不想为UkadcHtmlAttributeProvider这样的每种数据类型编辑一个editortemplate。

1 个答案:

答案 0 :(得分:2)

您可以定义包装原生扩展方法的自定义扩展方法:

public static class MyHtmlExtensions 
{
    public static MvcHtmlString MyEditorFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)        
    {
        return html.EditorFor(expression, new { htmlAttributes = new { @class = "form-control", id=""}});
    }
}

然后,在您看来:

@Html.MyEditorFor(model => model.AccountNumber);