2

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;
}
1
  • Are you passing the attributes to your code? It will ignore the attributes since you don't pass it. Commented May 19, 2011 at 9:52

1 Answer 1

2

Well, you're not using your htmlAttributes arg anywhere in the function.

Don't you need something like...

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

Also, you don't need the @ char infront of the tabindex, maxlength and size attributes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.