2015-09-25 49 views
0

我想在表單中禁用HTML id,因爲我在一個頁面上使用多個Ajax表單。 目前,我正在做這樣的:全局添加htmlAttributes到EditorFor MVC5

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

但我覺得必須有一個方式在全球範圍內聲明它,就像在一個自定義視圖引擎配置。我不喜歡在每個編輯器上都這麼做,而且我不喜歡爲像UkadcHtmlAttributeProvider這樣的每種數據類型創建編輯模板。

回答

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); 
+0

謝謝,去試試。試圖做一個自定義的EditorFor,但沒有嘗試包裝現有的。 – Jogai