我在ASP.NET MVC 4中有一个项目,最近使用Bootstrap进行了视觉改进。向asp.net上的所有html.textbox添加相同的类mvc
虽然我对此很满意,但有人认为我不是很满意。
我不得不添加
new { @class = "form-control input-sm" }
我所有的Html.TextBox助手的电话。我想知道是否有一个更优雅的方式来做到这一点...也许重写默认的TextBox助手?
任何有识之士都会非常感激。
我在ASP.NET MVC 4中有一个项目,最近使用Bootstrap进行了视觉改进。向asp.net上的所有html.textbox添加相同的类mvc
虽然我对此很满意,但有人认为我不是很满意。
我不得不添加
new { @class = "form-control input-sm" }
我所有的Html.TextBox助手的电话。我想知道是否有一个更优雅的方式来做到这一点...也许重写默认的TextBox助手?
任何有识之士都会非常感激。
编辑
它实际上是可行的 - 要覆盖扩展方法。参考见:How to override an existing extension method。
在“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)
}
会生成文本框,你“的形式控制输入-SM “css类。
我想你应该简单地使用jquery为输入字段添加类。因为你必须在许多plcaces再次更改代码文本框
$('input[type=text]').addClass('form-control input-sm');
添加自定义辅助类不会是有益的。
作为例子,如果这是你的自定义帮手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" })
}
}
你必须TextBoxFor
转换成该MyTextBoxFor
@Html.MyTextBoxFor(model => model.FirstName)
这就是为什么我想知道重写实际的方法。我喜欢在所有项目中使用Bootstrap的想法,我希望它对其他开发人员来说是透明的,所以如果我在通用项目中更改TextBox方法的行为,他们只会看到现在我们的输入更“用户友好”。 您的解决方案是一个好主意,看起来很公平,但在接受它之前,我会先深入了解我的第一个选项。 – Bardo
似乎没有办法重写保持相同签名的Html Helper,而无需在不同名称空间上完全重建整个库,因此您的解决方案是最干净的。不是我想要申请的人,但肯定是一个有效的麻烦免费的! – Bardo
编辑完成后,我更改了Ondrej的一个答案,因为他终于找到了我正在寻找的东西。 你的答案也是有效的(并涉及更少的代码),但我认为他的答案更清晰。 我想批准这两个有效答案:( – Bardo
这可能是一个不错的选择,但是我想将解决方案扩展到我们所有的项目中,并且希望尽可能透明地解决我们的其他开发人员的问题,当然也尽量简化他们的工作。那么,有什么方法可以改变实际的TextBox的方法行为?如果我可以这样做,那么我只需要将更改复制到其余项目中,然后放入我们的通用框架中,所有更新都不必更改更多代码。 – Bardo
@Bardo - 看我的编辑。 –
这正是我所期待的。我已经测试过它并且完美地工作。 我认为可能是一个常见问题的最佳解决方案。 – Bardo