Asp.net-mvc – How to specify attributes for a Html.TextBox helper while maintaining the value retrieval from ViewData

asp.net-mvc

I am using the Html.TextBox helper to create textboxes. I want to set attributes on the textbox, which I understand is done using the following overload:

Html.TextBox (string name, object value, object htmlAttributes)

However, I want to maintain the functionality where the HTML helper automatically uses the value from either ViewData or ViewData.Model and I do not see a way to just specify the name and the htmlAttributes. Is this possible?

Best Answer

[EDIT] After looking at the source code, it appears that all you need to do is specify the value as null in the signature that takes a name, value, and htmlAttributes. If the value is null, it will attempt to use the value from the ViewData.

Html.TextBox( "name", null, new { @class = "css-class" } );