使用 HtmlAttributes 覆盖 ASP.NET MVC 中的 TextBoxFor Helper
我正在尝试在 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;
}
Am Trying to use custom TextBoxFor in ASP.NET MVC 3 to change some existing attributes.
While rendering,
@Html.MYTextBoxFor(model => model.FirstName, new { @class = "textfield", @tabindex = "1", @maxlength = "50", @size = "30" })
But it ignores the 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您没有在函数中的任何地方使用
htmlAttributes
arg。你不需要像...
另外,你不需要 tabindex、maxlength 和 size 属性前面的
@
字符。Well, you're not using your
htmlAttributes
arg anywhere in the function.Don't you need something like...
Also, you don't need the
@
char infront of the tabindex, maxlength and size attributes.