This is a migrated thread and some comments may be shown as answers.

[Solved] DatePicker and nullable DateTimes

4 Answers 229 Views
Date/Time Pickers
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ehryk
Top achievements
Rank 2
Ehryk asked on 20 Nov 2011, 02:27 PM
I'm having a hard time with a datepicker and a nullable DateTime in my Telerik MVC Grid.  I only got it 'kind of' working in a certain way, and even then I can't open the datepicker when it's been set to null.

What I REALLY wanted was

columns.Bound(o => o.BeginDate).Title("BeginDate"); //Won't work
columns.Bound(o => o.BeginDateText).Title("BeginDateText"); //My hack to make it display, but I wanted it editable with a datepicker.

I wish there was a way to make the grid's columns editable by choice, columns.Bound().Edit(true);  But I cannot.  So, on to this:

columns
       .Template(
        @<text>
        @Html.Telerik().DatePicker().Name("dbBeginDate").Value(item.BeginDate)
        </text>)
        .Title("Begin Date");

However, as I mentioned, when it is bound to a null value, the box won't open.  What can I do here?

4 Answers, 1 is accepted

Sort by
0
Ehryk
Top achievements
Rank 2
answered on 20 Nov 2011, 02:33 PM
Ah, I see part of it:

I have to name each one differently in the grid.  Is there a better way to do this that I'm missing?
0
Ehryk
Top achievements
Rank 2
answered on 20 Nov 2011, 02:41 PM
Now I'm stuck on how to write a DatePicker in the ClientTemplate.  Do you have an example of a DatePicker inside a Grid Template/ClientTemplate working properly?
0
Marc
Top achievements
Rank 1
answered on 13 Dec 2011, 11:44 PM
Hey Ehryk,

Are you just trying to get a Datepicker to display every time you try to edit or are you trying to have it render with the grid?
For the first option, the easy solution is to use an EditorTemplate, the example provided in the demo has one. The way you implement that is to create a folder in your shared views called "EditorTemplates" and to create a "Date.cshtml" view in it - What this does is any time you use @Html.EditorFor (Or the grid inline derivative) it'll replace your control with the datepicker. Very handy.

As for the second option, I'm not entirely sure as to how to do that, but it would probably be something like

columns.Template(
@<text>
@{ Html.Telerik().DatePicker(); }
</text> );
columns.Bound(x=>x.Id);
//Etc

Note that's just an example as to introduce razor syntax into a Telerik Grid column, that won't actually get you a datepicker without you binding something to its' value attribute and rendering it. Perhaps one of the Telerik developers can shed some light on that, as I don't really know how one would go about doing that (As I'm only really familiar with having it happen from an edit).
0
Troy Demet
Top achievements
Rank 2
answered on 21 Feb 2012, 09:28 PM
I created a Editor Template for nullable date

var NullableDate = {
    dateTime_onLoad : function () {
        if ($("#" + this.id).val() == "01/01/0001 00:00:00" || $("#" + this.id).val() == "1/1/2001") {
            $("#" + this.id).val("");
        }       
    }
};

@ModelType System.Nullable(Of DateTime)
@Html.Telerik().DatePicker().Name( _
    ViewData.TemplateInfo.GetFullHtmlFieldName(String.Empty) _
    ).HtmlAttributes( _
    New With {.id = ViewData.TemplateInfo.GetFullHtmlFieldName(String.Empty) + "_wrapper"} _
    ).Value(If(Model > DateTime.MinValue, Model, DateTime.MinValue) _
            ).ClientEvents(Function(events) events.OnLoad("NullableDate.dateTime_onLoad"))

Tags
Date/Time Pickers
Asked by
Ehryk
Top achievements
Rank 2
Answers by
Ehryk
Top achievements
Rank 2
Marc
Top achievements
Rank 1
Troy Demet
Top achievements
Rank 2
Share this question
or