默认情况下,MVC 的输入 html 帮助程序生成具有相同值的“id”和“name”。就我而言,我不需要 ID,当我需要时,我总是输入自定义值。自动生成不必要的 id 会增加文档大小,让我感到紧张,因为我可能在 CSS 或 JavaScript 代码中使用相同的 id。
我可以更改 html 帮助程序的默认行为吗,这样除非手动定义,否则不会生成 id。
我知道我可以告诉助手不要通过传递来生成 id new { id = "" },但这种方法对于大型数据输入类型的应用程序来说不是很方便。
有没有一种简单的方法来改变这种行为,或者我是否需要编写自定义 html 帮助程序?
使用,
Html.TextBoxFor(m => m.Text).RemoveNameIdAttribute()
public static MvcHtmlString RemoveNameIdAttribute(this MvcHtmlString helper)
{
if (helper == null)
throw new ArgumentNullException();
var element = helper.ToString();
var regex = new Regex("(id|name)[^'\"]*['\"][^'\"]*['\"]",RegexOptions.IgnoreCase | RegexOptions.Compiled);
return MvcHtmlString.Create(regex.Replace(element, ""));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2980 次 |
| 最近记录: |