49

In my model I have the following DataAnnotations on one of my properties

[Required(ErrorMessage = "*")]
[DisplayFormat(DataFormatString = "{0:d}")]
[DataType(DataType.Date)]
public DateTime Birthdate { get; set; }

The required annotation works great, I added the other 2 to try and remove the time. It gets bound to an input in the view using

<%=Html.TextBoxFor(m => m.Birthdate, new { @class = "middle-input" })%>

However whenever the view loads I still get the time appearing in the input box. Is there anyway to remove this using DataAnnotations?

2

3 Answers 3

91

The [DisplayFormat] attribute is only used in EditorFor/DisplayFor, and not by the raw HTML APIs like TextBoxFor.

Sign up to request clarification or add additional context in comments.

2 Comments

I am using MVC4 Razor syntax, the date field is rendered as @Html.EditorFor(m => m.IssueDate) and the formatting on the model is applied as [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}" )] but the date is still shown as 01/01/0001 12:00:00 AM
@bjan Did you set the ApplyFormatInEditMode property?
29

As Brad said it dosn't work for TextBoxFor but you'll also need to remember to add the ApplyFormatInEditMode if you want it to work for EditorFor.

[DataType(DataType.Date), DisplayFormat( DataFormatString="{0:dd/MM/yy}", ApplyFormatInEditMode=true )]
public System.DateTime DateCreated { get; set; }

Then use

@Html.EditorFor(model => model.DateCreated)

1 Comment

EditorFor threw me off. I had TextBoxFor - That worked for other formatting such as numbers or labels. +1
8

My problem was to set some html attributes (jquery-datepicker), so EditorFor was no option for me.

Implementing a custom helper-methode solved my problem:

ModelClass with DateTime-Property:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}", ApplyFormatInEditMode = true)]
public DateTime CustomDate{ get; set; }

View with ModelClass as Model:

@Html.TextBoxWithFormatFor(m => m.CustomDate, new Dictionary<string, object> { { "class", "datepicker" } })

Helper-Methode in static helper class:

public static class HtmlHelperExtension {
    public static MvcHtmlString TextBoxWithFormatFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes) {
        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        return htmlHelper.TextBox(htmlHelper.ViewData.TemplateInfo.GetFullHtmlFieldName(metadata.PropertyName), string.Format(metadata.DisplayFormatString, metadata.Model), htmlAttributes);
    }
}

1 Comment

You could keep using EditorFor if you just create an Editor template for DateTime that does what you need it to do instead. Editor templates are nice because from the view you can request an Editor without having to know too much about what you're going to get, and you can change all the editors out for different controls at will by just changing the editor templates.

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.