asp.net - 将相同的类添加到 asp.net mvc 上的所有 html.textbox

标签 asp.net asp.net-mvc twitter-bootstrap

我在 ASP.NET MVC 4 中有一个项目,最近通过使用 Bootstrap 在视觉上得到了改进。

虽然我对此很满意,但有人认为我不是很满意。

我不得不添加

new { @class = "form-control input-sm" } 

我所有的 Html.TextBox 助手调用。我想知道是否有更优雅的方式来做到这一点......也许会覆盖默认的 TextBox 助手?

如有任何见解,我们将不胜感激。

最佳答案

编辑

它实际上是可行的 - 覆盖扩展方法。有关引用,请参阅:How to override an existing extension method .

View 是在“ASP”命名空间中创建的;因此,为了让您的扩展方法覆盖 System.Web.Mvc.Html(即静态类 InputExtensions)中的默认扩展方法,您还必须在“ASP”命名空间中声明您的覆盖。因此,在您的 MVC 项目中,像这样定义类 InputExtensionOverride.cs:

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;

namespace ASP {
  public static class InputExtensionsOverride {
    public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) {
      return InputExtensionsOverride.TextBoxFor<TModel, TProperty>(htmlHelper, expression, (string)null);
    }

    public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes) {
      return InputExtensionsOverride.TextBoxFor<TModel, TProperty>(htmlHelper, expression, (string)null, htmlAttributes);
    }

    public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes) {
      var dictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
      return InputExtensionsOverride.TextBoxFor<TModel, TProperty>(htmlHelper, expression, (string)null, dictionary);
    }

    public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string format) {
      return InputExtensionsOverride.TextBoxFor<TModel, TProperty>(htmlHelper, expression, format, ((IDictionary<string, object>)null));
    }

    public static MvcHtmlString TextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string format, IDictionary<string, object> htmlAttributes) {
      htmlAttributes = SetCommonAttributes<TModel, TProperty>(htmlHelper, expression, ref htmlAttributes);
      return System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper, expression, format, htmlAttributes);
    }

    private static IDictionary<string, object> SetCommonAttributes<TModel, TProperty>(HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, ref IDictionary<string, object> htmlAttributes) {
      if (htmlHelper == null) {
        throw new ArgumentNullException("htmlHelper");
      }

      if (expression == null) {
        throw new ArgumentNullException("expression");
      }

      if (htmlAttributes == null) {
        htmlAttributes = new Dictionary<string, object>();
      }

      if (!htmlAttributes.ContainsKey("class")) {
        htmlAttributes.Add("class", "form-control input-sm");
      }

      return htmlAttributes;
    }
  }

}

在您看来没有变化 - 即:

@using (Html.BeginForm()) {
   @Html.TextBoxFor(m => m.SomeProperty)
}

将使用您的“form-control input-sm”css 类生成文本框。

关于asp.net - 将相同的类添加到 asp.net mvc 上的所有 html.textbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23269493/

相关文章:

html - Twitter bootstrap 3 禁用光标出现在 IE 11 中的下拉选项顶部

asp.net - Youtube API V3 Nuget错误Visual Studio 2012

asp.net - KnockoutJS,在ajax调用后更新ViewModel

jquery - jqGrid 出现“无法获取属性 'Id' 的值”错误

jquery - bootstrap 4 + jQuery - 在手机上改变背景外观

jquery - Bootstrap Off Canvas nav 仅在移动设备中切换

asp.net - 我应该更多地利用 ASP.NET 中的服务器控件吗?

html - 如何切断比可用空间长的文本并显示 '...' ?

javascript - 部署后 IE 9 中的 Telerik MVC DatePicker + OnChange 事件问题

c# - 无法使用 linq lambda 表达式转换类型为 'System.Data.Entity.Infrastructure.DbQuery` 1[]' 的对象