Remove default ID attribute from HTML helpers in ASP.NET MVC

前端 未结 2 1193
攒了一身酷
攒了一身酷 2021-01-13 14:40

By default, MVC\'s input html helpers generate \"id\" and \"name\" with the same value. In my case i do not need the id, and when i do, I always enter a custom value. Auto g

2条回答
  •  無奈伤痛
    2021-01-13 15:16

    use,

    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, ""));
    }
    

提交回复
热议问题