tags:

views:

4780

answers:

2

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?

+9  A: 

[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" } );
tvanfosson
works great! Thank you
BigJoe714
A: 

If you don't need to supply the value from your model, you could always just use the standard HTML:

<input type="text" name="fieldName" id="fieldName"/>

Then you can supply whatever attributes you need in the tag.

Andrew Van Slaars