Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add the css class attribute to a textbox in razor?

I have tried:

@Html.TextBoxFor(m => m.UserName, new {@class='textbox'}) 

which isn't working.

like image 807
Magnus Gladh Avatar asked Oct 23 '10 21:10

Magnus Gladh


People also ask

How do I add a class to razor form?

You can set the class with @class="className" but you have to specify actionName, controllerName, FormMethod too because there is no overload of Html. BeginForm that supports setting only html attributes. You can create your own html helper, that does that for you, too. Here is a custom html helper does that.

What is CSS class attribute?

The HTML class attribute specifies one or more class names for an element. Classes are used by CSS and JavaScript to select and access specific elements. The class attribute can be used on any HTML element. The class name is case sensitive. Different HTML elements can point to the same class name.

How do you use a class in razor view?

Just use a view model. If you are, you can make List<ObjectData> part of it. In your controller, you load up that list (lets call it ObjectDataList ), and send it to your view.


1 Answers

@Html.TextBoxFor(m => m.UserName, new {@class="textbox"}) 

A c# string literal does not take single quotes.

'textbox' -> "textbox"

like image 157
takepara Avatar answered Oct 19 '22 20:10

takepara