14

Is it possible to set a css class in a TextBox html helper? This doesn't compile, obviously:

<%=Html.TextBox("Region",Model.Region,new {class="Autocomplete"}) %>

Thanks.

1 Answer 1

23

Class is a reserved keyword, so you need to write it like this:

<%=Html.TextBox("Region", Model.Region, new { @class="Autocomplete" }) %>
Sign up to request clarification or add additional context in comments.

4 Comments

This is great! I love the fact that google is indexing these pretty well now. I found this solution within 60 seconds of asking myself "how do I set the class attribute?"
How do you add multiple classes? e.g., <p class="someclass someotherclass">paragraph</p>. That's html valid but when I try to add it using the new { @class="xyz abc" } neither class shows up.
@shubniggurath There must be something wrong on your side. That's the proper way to have multiple CSS-classes.
@SebNilsson - great! Thanks. I did two or three web searches on "multiple css classes in html.dropdownlist" etc and couldn't find a good answer. If that's right then I'll figure out what's wrong with my code. Thanks, thanks, thanks.

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.