1

I'm passing a model to a view, and the model contains this attribute:

[Required(ErrorMessage = "Please enter a start date")]
[DataType(DataType.DateTime)]
[DisplayName("Start Date")]
public DateTime StartDate { get; set; }

And I have this in my view:

<%: Html.TextBoxFor(m => m.StartDate) %>

When I load the page, the textbox is populated with a date: 1/1/0001 12:00:00 AM

Is there a way to not have this happen, maybe by using metadata?

2 Answers 2

5

In the constructor, programmatically set the StartDate property to the date you want to use as the default. (Such as DateTime.Today.)

If you want the value to be initially empty, try using a Nullable<DateTime> which will default to null.

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

Comments

0

Use DateTime? ASP.net MVC Data Annotations DateTime Default Value

Comments

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.