ASP.NET MVC 3 Razor - Adding class to EditorFor
Adding a class to Html.EditorFor
doesn't make sense as inside its template you could have many different tags. So you need to assign the class inside the editor template:
@Html.EditorFor(x => x.Created)
and in the custom template:
<div>
@Html.TextBoxForModel(x => x.Created, new { @class = "date" })
</div>
As of ASP.NET MVC 5.1, adding a class to an EditorFor
is possible (the original question specified ASP.NET MVC 3, and the accepted answer is still the best with that considered).
@Html.EditorFor(x=> x.MyProperty,
new { htmlAttributes = new { @class = "MyCssClass" } })
See: What's New in ASP.NET MVC 5.1, Bootstrap support for editor templates
You can't set class for the generic EditorFor. If you know the editor that you want, you can use it straight away, there you can set the class. You don't need to build any custom templates.
@Html.TextBoxFor(x => x.Created, new { @class = "date" })
You can use:
@Html.EditorFor(x => x.Created, new { htmlAttributes = new { @class = "date" } })
(At least with ASP.NET MVC 5, but I do not know how that was with ASP.NET MVC 3.)
I had the same frustrating issue, and I didn't want to create an EditorTemplate that applied to all DateTime values (there were times in my UI where I wanted to display the time and not a jQuery UI drop-down calendar). In my research, the root issues I came across were:
- The standard TextBoxFor helper allowed me to apply a custom class of "date-picker" to render the unobtrusive jQuery UI calender, but TextBoxFor wouldn't format a DateTime without the time, therefore causing the calendar rendering to fail.
- The standard EditorFor would display the DateTime as a formatted string (when decorated with the proper attributes such as
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
, but it wouldn't allow me to apply the custom "date-picker" class.
Therefore, I created custom HtmlHelper class that has the following benefits:
- The method automatically converts the DateTime into the ShortDateString needed by the jQuery calendar (jQuery will fail if the time is present).
- By default, the helper will apply the required htmlAttributes to display a jQuery calendar, but they can be overridden if needs be.
- If the date is null, ASP.NET MVC will put a date of 1/1/0001 as a value.
This method replaces that with an empty string.
public static MvcHtmlString CalenderTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes = null)
{
var mvcHtmlString = System.Web.Mvc.Html.InputExtensions.TextBoxFor(htmlHelper, expression, htmlAttributes ?? new { @class = "text-box single-line date-picker" });
var xDoc = XDocument.Parse(mvcHtmlString.ToHtmlString());
var xElement = xDoc.Element("input");
if (xElement != null)
{
var valueAttribute = xElement.Attribute("value");
if (valueAttribute != null)
{
valueAttribute.Value = DateTime.Parse(valueAttribute.Value).ToShortDateString();
if (valueAttribute.Value == "1/1/0001")
valueAttribute.Value = string.Empty;
}
}
return new MvcHtmlString(xDoc.ToString());
}
And for those that want to know the JQuery syntax that looks for objects with the date-picker
class decoration to then render the calendar, here it is:
$(document).ready(function () {
$('.date-picker').datepicker({ inline: true, maxDate: 0, changeMonth: true, changeYear: true });
$('.date-picker').datepicker('option', 'showAnim', 'slideDown');
});