0

My model has a property like this:

[Display(Name = "Date of Service")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] // ISO compliance for jQuery validation
[SqlDateRange] // Enforces min/max SQL DateTime values (also handles nulls caused by invalid dates like "12/01/19900" -- I suppose the issue could be resolved by finding a better way of handling that null case
public DateTime? DateOfService { get; set; }

But when I use this in my view,

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

I get this, which gets passed as null. I'm already checking for null via an attribute because the editor also allows hugely invalid dates like 12/12/21390 -- which also pass as null.

Edit: I'm using Chrome.

Sinjai
  • 1,085
  • 1
  • 15
  • 34
  • Have you tried `model => model.Date.Value`? Or create your own `DateTime?` editor template https://stackoverflow.com/questions/8580780/editorfor-on-nullable-datetime-nullable-object-must-have-a-value – Jasen Jun 20 '17 at 01:49
  • What you seeing is a placeholder assuming your using Chrome or Edge and its the browsers HTML-5 datepicker implementation. What data annotations are applied to your property and which browser are you using –  Jun 20 '17 at 02:12
  • @Jasen Don't think that question's actually relevant. – Sinjai Jun 20 '17 at 02:15
  • @StephenMuecke Yeah, I know it's a placeholder. I just don't know how to allow the null when it's empty but not allow it when it's just an invalid date (which could happen accidentally). See edit. – Sinjai Jun 20 '17 at 02:26
  • What is your `[SqlDateRange]` attribute. And I do not understand your comment. If you do not enter a valid date of course it will be `null` because the `DefaulrModelBinder` cannot set the property. Are you wanting to limit entry to a specific range, and show a client side validation error if so? –  Jun 20 '17 at 02:31
  • The form allows you to enter a date like 12/01/83672. The custom attribute causes validation to fail in that case, or if it's a valid date outside SQL's date range (1753-9999). – Sinjai Jun 20 '17 at 02:33
  • So what is your problem then? If you have implemented that correctly for client side validation, your form wont even submit –  Jun 20 '17 at 02:53
  • @StephenMuecke It's an optional field, it should be submitable when empty. I can't differentiate between the two null values. – Sinjai Jun 20 '17 at 02:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147119/discussion-between-stephen-muecke-and-sinjai). –  Jun 20 '17 at 03:02

1 Answers1

0

All you have to do is type format you want in html. I hope it will work for you.

@Html.EditorFor(model => model.Date, "{0:dd/MM/yyyy}")
Tien Nguyen Ngoc
  • 1,555
  • 1
  • 8
  • 17
  • It already has a format via an attribute. `[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] ` Needs to comply with ISO because of jQuery, as I recall. Not actually sure why it doesn't ultimately display like that. Regardless, why would this have any effect? – Sinjai Jun 20 '17 at 02:14
  • You can see https://stackoverflow.com/questions/35944081/date-format-dd-mm-yyyy-not-working-in-asp-net-mvc-5. It can be helpful for you. – Tien Nguyen Ngoc Jun 20 '17 at 02:40
  • Do you understand my question? – Sinjai Jun 20 '17 at 02:43
  • Yes, I do. I can not help you why it was not shown. I found other problem that is the same your problem. You can see that problem. – Tien Nguyen Ngoc Jun 20 '17 at 02:47
  • That's the thing though, those other problems are not the same as mine. – Sinjai Jun 20 '17 at 04:39