namespace System.Web.Mvc { public static class HtmlHelperExtensions { public static MvcHtmlString BsTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string placeholder,string @class) { TagBuilder tagBuilder = new TagBuilder("input"); ModelMetadata metadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData); string name = ExpressionHelper.GetExpressionText(expression); tagBuilder.GenerateId(name); tagBuilder.MergeAttribute("name", name, true); tagBuilder.MergeAttribute("type", "text"); tagBuilder.MergeAttribute("placeholder", placeholder); tagBuilder.MergeAttributes<string, object>(htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata)); tagBuilder.AddCssClass(@class); return new MvcHtmlString(tagBuilder.ToString()); } } }
放到 MVC Web 项目中。 placeholder 文本框中 提示的内容, class 属性 ,一般采用 Bootstrap 时 ,class 很好用