1

I use MVC 5 and in my model I have DateTime property like this:

[Column(TypeName = "Date")]
[DataType(DataType.Date)]
public DateTime StartDate { get; set; }

when I entered an invalid date the following message appears in ValidationSummary:

The field StartDate must be a date

I need to change this message where I can change it?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Abraham Josef
  • 653
  • 1
  • 8
  • 30

3 Answers3

4

You need to add ErrorMessage property .Try this

[DataType(DataType.Date),ErrorMessage="Your message here"]
Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40
  • 3
    It looks obvious, but that doesn't work. http://stackoverflow.com/questions/13916991/mvc-datatype-errormessage – CodeCaster Jun 01 '15 at 13:23
  • @CodeCaster yes ErrorMessage doesn't work with me, but the link you sent didn't help me as well please I need more clarification what is the use of ErrorMessage in DataType and how can I override this default message – Abraham Josef Jun 01 '15 at 13:32
  • @AbrahamJosef scroll down a bit to the next answer in that link that CodeCaster provided. It shows how you can use [`ValidationMessageFor`](https://msdn.microsoft.com/en-us/library/ee703502(v=vs.118).aspx). Hth... – EdSF Jun 01 '15 at 13:51
1

What worked for me was adding a data-val-date attribute to the textbox to override the one that gets created for it (inspect the textbox and you'll see).

@Html.TextBoxFor(model => model.DateOfBirth, new { @class = "form-control", inputDate = "", data_val_date = "Please enter a valid date" })
oddmin
  • 11
  • 1
0

From razor you can do this in this way.

@Html.ValidationMessageFor(model => model.StartDate, "Your custom message.", new { @class = "text-danger" })
Maske
  • 824
  • 2
  • 17
  • 35