【问题标题】:Overwrite TextBoxFor Helper in ASP.NET MVC with HtmlAttributes用 HtmlAttributes 覆盖 ASP.NET MVC 中的 TextBoxFor Helper
【发布时间】: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


【解决方案1】:

好吧,你没有在函数的任何地方使用你的 htmlAttributes arg。

你不需要类似...

MvcHtmlString normal = html.TextBoxFor(expression, htmlAttributes);

此外,您不需要 tabindex、maxlength 和 size 属性前面的 @ 字符。

【讨论】:

    猜你喜欢
    • 2011-08-28
    • 1970-01-01
    • 2016-10-17
    • 2011-09-14
    • 1970-01-01
    • 2013-10-15
    • 2011-10-01
    • 1970-01-01
    • 2011-12-14
    相关资源
    最近更新 更多