Bar*_*rdo 2 asp.net asp.net-mvc twitter-bootstrap
我在ASP.NET MVC 4中有一个项目,最近使用Bootstrap进行了视觉上的改进.
虽然我对它很满意,但有人认为我并不十分满意.
我不得不补充一下
new { @class = "form-control input-sm" }
Run Code Online (Sandbox Code Playgroud)
对我所有的Html.TextBox助手调用.我想知道是否有更优雅的方法来做到这一点......可能会覆盖默认的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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在您看来,没有变化 - 即:
@using (Html.BeginForm()) {
@Html.TextBoxFor(m => m.SomeProperty)
}
Run Code Online (Sandbox Code Playgroud)
将生成带有"form-control input-sm"css类的文本框.
| 归档时间: |
|
| 查看次数: |
909 次 |
| 最近记录: |