Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling HTML helpers ASP.NET MVC

Tags:

If I have an HTML helper like so:

Name:<br /> <%=Html.TextBox("txtName",20) %><br /> 

How do I apply a CSS class to it? Do I have to wrap it in a span? Or do I need to somehow utilize the HtmlAttributes property of the helper?

like image 429
Dan Avatar asked Sep 02 '08 22:09

Dan


People also ask

What are HTML helpers in MVC ?-?

In MVC, HTML Helper can be considered as a method that returns you a string. This string can describe the specific type of detail of your requirement. Example: We can utilize the HTML Helpers to perform standard HTML tags, for example HTML<input>, and any <img> tags.

How many types of HTML helpers are there in MVC?

There are three types of built-in HTML helpers offered by ASP.NET.

What are HTML helpers in MVC ?- 5 marks?

What is HTML Helper in ASP.NET MVC 5? HTML Helpers are methods that return a string. Helper class can create HTML controls programmatically. HTML Helpers are used in View to render HTML content.


1 Answers

You can pass it into the TextBox call as a parameter.

Name:<br/>     <%= Html.TextBox("txtName", "20", new { @class = "hello" }) %> 

This line will create a text box with the value 20 and assign the class attribute with the value hello. I put the @ character in front of the class, because class is a reserved keyword. If you want to add other attributes, just separate the key/value pairs with commas.

like image 200
Dale Ragan Avatar answered Oct 20 '22 23:10

Dale Ragan