Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在asp.net mvc上向所有html.textbox添加相同的类_Asp.net_Asp.net Mvc_Twitter Bootstrap - Fatal编程技术网

在asp.net mvc上向所有html.textbox添加相同的类

在asp.net mvc上向所有html.textbox添加相同的类,asp.net,asp.net-mvc,twitter-bootstrap,Asp.net,Asp.net Mvc,Twitter Bootstrap,我在ASP.NETMVC4中有一个项目,它最近通过使用引导程序在视觉上得到了改进 虽然我对它很满意,但有一个我觉得不太满意 我不得不补充一点 new { @class = "form-control input-sm" } 我的所有Html.TextBox帮助程序调用。我想知道是否有更优雅的方式来做到这一点。。。可能会覆盖默认的文本框帮助器 如果您对此有任何见解,我们将不胜感激。编辑 它实际上是可行的——覆盖扩展方法。有关参考信息,请参阅: 在“ASP”命名空间中创建视图;因此,为了让扩展方

我在ASP.NETMVC4中有一个项目,它最近通过使用引导程序在视觉上得到了改进

虽然我对它很满意,但有一个我觉得不太满意

我不得不补充一点

new { @class = "form-control input-sm" } 
我的所有Html.TextBox帮助程序调用。我想知道是否有更优雅的方式来做到这一点。。。可能会覆盖默认的文本框帮助器

如果您对此有任何见解,我们将不胜感激。

编辑

它实际上是可行的——覆盖扩展方法。有关参考信息,请参阅:

在“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;
    }
  }

}

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

我认为您只需使用jquery为输入字段添加类即可。

$('input[type=text]').addClass('form-control input-sm');
为textbox添加自定义帮助器类不会有帮助,因为您必须在许多情况下再次更改代码

例如,如果这是您的自定义帮助器textboxfor

public static class MyHtmlHelpers
{
    public static MvcHtmlString MyTextBoxFor<TModel, TProperty>(
         this HtmlHelper<TModel> helper, 
         Expression<Func<TModel, TProperty>> expression)
    {
        return helper.TextBoxFor(expression, new { @class = "form-control input-sm" }) 
    }
}

这就是为什么我想重写实际的方法。我喜欢在我们所有的项目中使用引导的想法,并且我希望它对其他开发人员是透明的,所以如果我在我们的通用项目中改变TextBox方法的行为,他们只会看到现在我们的输入更加“用户友好”。您的解决方案是一个好主意,看起来很公平,但是在接受它之前,我将深入研究我的第一个选项。如果不在不同的命名空间上完全重建整个库,似乎无法覆盖维护相同签名的Html帮助器,因此您的解决方案是最干净的。不是我想申请的那个,而是一个有效的,没有麻烦的!在编辑后,我把答案改成了昂德雷的答案,因为他终于找到了我想要的东西。你的答案也是正确的(而且涉及的代码更少),但我认为他的答案更清晰。我想批准这两个答案作为有效答案:(这可能是一个很好的选择,但是我想将解决方案扩展到我们所有的项目,我想让它对我们的其他开发人员尽可能透明,当然,尝试简化他们的工作。那么,有没有一种方法可以改变实际TextBox的方法行为?如果可以的话,我只需要在到其余的项目和我们的通用框架,所有都将更新,而无需更改更多的代码。这正是我所寻找的。我已经测试过它,工作完美。我认为这可能是一个常见问题的伟大解决方案。强调ASP命名空间。在MVC5中添加Bootstrap 3类非常好。谢谢!
public static class MyHtmlHelpers
{
    public static MvcHtmlString MyTextBoxFor<TModel, TProperty>(
         this HtmlHelper<TModel> helper, 
         Expression<Func<TModel, TProperty>> expression)
    {
        return helper.TextBoxFor(expression, new { @class = "form-control input-sm" }) 
    }
}
@Html.MyTextBoxFor(model => model.FirstName)