Add css class to Html.EditorFor in MVC 2

asp.net-mvc, c#

Solution

I would HIGHLY suggest using Editor Templates. It's definitely the "right" way to style your EditorFor.

You can tell a model property to use an Editor Template in two different ways.

The first (the simplest) is to create an editor template for a certain data type - `DateTime` for example. The second way to do it is to set it declaratively in your DataAnnotations by using a UIHint.

Edit I'd also like to add that you should use the "date" type in your input field so that even when JavaScript is disabled, your user can stills see a native datepicker (only valid on modern HTML5 browsers) `<input id="meeting" type="date" value="2011-01-13"/>`

Problem

I'm trying to add a css class to a textbox. This is what I have in my view: ``` <%: Html.EditorFor(m => m.StartDate) %> ``` I tried following the instructions at this link by making my code: ``` <%: Html.EditorFor(m => m.StartDate, new { @class: "datepicker" }) %> ``` But I get a compiler error saying: Syntax error, ',' expected What am I doing wrong here?

Original source

Related problems