10

I need the default value of datetime picker to be empty.

@(Html.Kendo().DatePickerFor(model => model.RevisionDate)
              .Name("RevisionDate")
                )

"RevisionDate" is a datetime property in a asp.net mvc model.

how it's currently being displayed

enter image description here

and i need to display it as below

enter image description here

chamara
  • 12,649
  • 32
  • 134
  • 210

4 Answers4

22

Your RevisionDate property is by default set to DateTime.MinValue which is 01/01/0001 ( all DateTime properties behave like that). This is why the Kendo UI DatePicker shows it like that. The solution is to make the property a nullable DateTime (DateTime?) whose default value is null.

Atanas Korchev
  • 30,562
  • 8
  • 59
  • 93
  • 1
    I have this same issue except I haven't bound the DateTimePicker to a model property but just gave it a name. I haven't figured out my issue yet. – janoulle May 28 '14 at 19:06
5

If you don't want to make the property nullable, you can also do it like this:

@(Html.Kendo().DatePickerFor(m => m.Property).HtmlAttributes(new { value = "" }))
mctl87
  • 351
  • 8
  • 16
  • 2
    The one problem with this is that if, for example, there are Model state errors and the Post returns the errors, the date field does not show it as it's value attribute is blanked out – Dave Sep 12 '16 at 12:30
1

I had the same problem with a ComboBoxFor ComboBoxFor(m => m.LocationId).Value(Model.LocationId == 0 ? string.Empty : Model.LocationId.ToString()).

But I see that this approach doesn't work on the DatePickerFor(It could be a bug).

A workaround could be made in JS if($('#RevisionDate ').val() == "1/1/0001") { $('#RevisionDate ').val("") } but I would recommand Atanas's approach with a nullable field.

Misi
  • 748
  • 5
  • 21
  • 46
0

Blank out the field from the client side using something like this in your markup/cshtml file :

<script>
    // Blank out the 1/1/0001 12:00 AM from the date field
    $('#RevisionDate').val("");
 </script>
Plan Man
  • 23
  • 1
  • 8