【发布时间】:2011-05-19 09:41:55
【问题描述】:
我正在尝试在 ASP.NET MVC 3 中使用自定义 TextBoxFor 来更改一些现有属性。
渲染时,
@Html.MYTextBoxFor(model => model.FirstName, new { @class = "textfield", @tabindex = "1", @maxlength = "50", @size = "30" })
但它忽略了 htmlAttributes(tabindex,maxlength,size)。
public static MvcHtmlString MYTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
{
string elementName = ExpressionHelper.GetExpressionText(expression);
MvcHtmlString normal = html.TextBoxFor(expression);
if (normal != null)
{
string newValidator = normal.ToHtmlString();
newValidator = newValidator.Replace("data-val-required", "databvalidatormsg");
return MvcHtmlString.Create(newValidator);
}
return null;
}
【问题讨论】:
-
您是否将属性传递给您的代码?它会忽略属性,因为你没有传递它。
标签: c# .net asp.net-mvc html-helper