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

[Solved] Nullable Types causes Column Binding problem (version 2010.3.1110)

9 Answers 348 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Derek
Top achievements
Rank 1
Derek asked on 11 Nov 2010, 07:08 PM
Since upgrading from 2010.2.713 to the latest 2010.3.1110 I'm having an issue with Nullable DateTime fields bound to the Telerik MVC Grid.

This appears to be a breaking change with this release as I haven't modified any code - I simply ran the Telerik upgrade wizard and my grid no longer works.

The error I get is this:-
"The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.DateTime'."

This only happens when I'm explicitly setting the Bound Columns. If I don't supply a set of column definitions and just let the grid work out what the columns are from the model data, then the nullable DateTime field displays correctly.  So the problem isn't with my model or the data but appears to be a bug with the column binding in the latest version.

Back to this specific example... I'm using this segment of code to set the columns:-

<%= Html.Telerik().Grid<Vortal.Models.UserData>(Model.UserData)
            .ClientEvents(events => events.OnError("GridEdit_ValidationError"))
            .Name("UserDataGrid")
            .DataKeys(dataKeys => dataKeys.Add(c => c.UserID))
            .DataBinding(dataBinding => dataBinding
                            .Ajax()
                                .Select("_AjaxGridSelect", "Admin", new { targetRoleFilter = Model.filterTargetRoleID })
                                .Update("_AjaxGridUpdate", "Admin", new { targetRoleFilter = Model.filterTargetRoleID })
                                .Delete("_AjaxGridDelete", "Admin", new { targetRoleFilter = Model.filterTargetRoleID })
                        )
            .Columns(columns =>
                {                   
                    columns.Command(commands =>
                    {
                        commands.Edit();
                        commands.Delete();
                    }).Title("Commands").Width("18%");
                                                               
                    columns.Bound(c => c.Title).Width("4%");
                    columns.Bound(c => c.Firstname).Width("8%");
                    columns.Bound(c => c.Middlename).Width("8%").Title("Middle");
                    columns.Bound(c => c.Surname).Width("11%");
                    columns.Bound(c => c.Company).Width("11%");
                    columns.Bound(c => c.Mobile).Width("9%");
                    columns.Bound(c => c.Email).Width("18%");
                    columns.Bound(c => c.DateOfBirth).Format("{0:dd-MM-yyyy}").Width("9%").Title("DOB");
                    columns.Bound(c => c.Sex).Width("4%");
                    columns.Bound(c => c.CCID).Width(0).Hidden(true);
                    columns.Bound(c => c.UEID).Width(0).Hidden(true);
                    columns.Bound(c => c.UPID).Width(0).Hidden(true);                   
                })                              
            .Editable(settings => settings.Enabled(true))
            .Pageable()
            .Filterable()    
            .Resizable(settings => settings.Columns(true))         
            .Sortable(sorting => sorting.OrderBy(sortOrder => sortOrder.Add(o=>o.UserID)))                                                 
    %>


Again note that the above code used to work just fine. Simply upgrading to the latest version of Telerik MVC without my making any code changes causes this to break!

In the new version 2010.3.1110, I can make the code work if I remove the nullable DateTime field (c.DateOfBirth) from the list of output columns 
e.g.
If I do this (i.e. remove the DateOfBirth column):-

.Columns(columns =>
                {                   
                    columns.Command(commands =>
                    {
                        commands.Edit();
                        commands.Delete();
                    }).Title("Commands").Width("18%");
                                                               
                    columns.Bound(c => c.Title).Width("4%");
                    columns.Bound(c => c.Firstname).Width("8%");
                    columns.Bound(c => c.Middlename).Width("8%").Title("Middle");
                    columns.Bound(c => c.Surname).Width("11%");
                    columns.Bound(c => c.Company).Width("11%");
                    columns.Bound(c => c.Mobile).Width("9%");
                    columns.Bound(c => c.Email).Width("18%");
                    columns.Bound(c => c.Sex).Width("4%");
                    columns.Bound(c => c.CCID).Width(0).Hidden(true);
                    columns.Bound(c => c.UEID).Width(0).Hidden(true);
                    columns.Bound(c => c.UPID).Width(0).Hidden(true);                   
                })

Then the grid shows up again correctly, however my date of birth field is obviously now missing, which is no good!

So is this a new bug with this latest version? And can it be patched fairly quickly or is there a workaround?

In general, what is the best way to handle optional dates? I want to avoid having to specify some default date (e.g. 1/1/1900) as this doesn't make sense in most scenarios, therefore the best option appears to be to use nullable (DateTime?) fields, as then the user can leave them blank if they like. (Note that converting the field to a string doesn't work either as then the sorting won't work correctly as it will sort alphabetically rather than chronologically.)

Any help or advice would be much appreciated!

Derek

9 Answers, 1 is accepted

Sort by
0
Frank
Top achievements
Rank 1
answered on 12 Nov 2010, 06:49 AM
Hi Derek,

I ran into exactly the same problem with a nullable DateTime field after upgrading from Q2.

Telerik Team,

Any advise/workaround/patch is indeed greatly appreciated. Thank you,

Frank.
0
Atanas Korchev
Telerik team
answered on 12 Nov 2010, 09:37 AM
Hello,

 In Q3 2010 we introduced a new editor template DateTime.ascx. It is located in ~/Views/Shared/EditorTemplates. The problem is its generic argument is DateTime not DateTime?.

You can try one of the following in order to solve that problem:

  1. Change the type of the model by editing DateTime.ascx
    Old:
    Inherits="System.Web.Mvc.ViewUserControl<DateTime>"
    New:
    Inherits="System.Web.Mvc.ViewUserControl<DateTime?>"
  2. Delete DateTime.ascx altogether


Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Frank
Top achievements
Rank 1
answered on 12 Nov 2010, 10:27 AM
Hi Atanas,

Thanks for the fast reply! That works!

I hadn't correlated this problem with the new template yet. I chose to use the first solution (i.e. update DateTime.ascx) as I prefer to test the new template out in some forms.

Thanks again for your help,

Frank.


0
Derek
Top achievements
Rank 1
answered on 12 Nov 2010, 11:43 AM
Hi Atanas
Thanks for the quick response!

Either of those two options you've given do indeed seem to fix the problem.

However, In my case I chose to remove the template, as I found that the DateTime Picker/PopUp wasn't working by default (possibly because I have some other configuration to do to get those to work?) and also because I found that in IE (only tried IE8) that it was giving a javascript error.  This might again be caused by not having the PopUp/DateTime Picker working in my case, but the easiest route for me was just to remove the template.

Any pointers/articles on getting the DateTime picker to actually work within the grid would be really helpful, or maybe this is just an issue with the current version of the grid perhaps?

Anyway, thanks for your help - at least I have everything back working the way it was now!

Derek
0
Accepted
Atanas Korchev
Telerik team
answered on 12 Nov 2010, 01:11 PM
Hello Derek,

 You are probably observing a recently discovered problem. You can try the build from this forum thread and see if it helps.

All the best,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
David Whiteley
Top achievements
Rank 1
answered on 20 Dec 2010, 07:43 PM
I have applied this fix and it solved the null date problem to some degree.  When I access a grid row that contains null dates, the default date is "1/1/1" which when updated turns into "1/1/2001".  The code in Date.aspx looks like it should select the current date for the default:

Html.Telerik().DatePicker()
        .Name(ViewData.TemplateInfo.GetFullHtmlFieldName(string.Empty))
        .Value(Model > DateTime.MinValue ? Model : DateTime.Today)

I can delete the ".Value" line above and it doesn't change the behavior of the date control.  I then changed the .Value to ".Value(DateTime.Today)" and this still has no effect.  As a sanity check, I put a breakpoint on this code and executed my program and the breakpoint was hit, so I know the code is being at least looked at.  However, the DatePicker continues to operate as though it is ignoring this line.

On a possibly related note, if I blank out the date on the grid and try to update, it turns red and says that the date is required.  How do I tell the grid that the date is not required?

0
Atanas Korchev
Telerik team
answered on 21 Dec 2010, 08:41 AM
Hello David Whiteley,

 To make a field not required decorate it with  [Required(false)]. As for your first question - we will need a sample project which demonstrates that issue. You can attach it to this forum thread.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
David Whiteley
Top achievements
Rank 1
answered on 24 Dec 2010, 02:40 PM
The Required attribute doesn't have a parameter that can be set false.  However, I did discover that some code in my service layer was replacing null values with DateTime.MinValue.  I made all versions of the DateTime field nullable in the path and everything started to work correctly.  Now the default date shows up as the current date.  Thank you.
0
Jeremy
Top achievements
Rank 1
answered on 21 Nov 2011, 07:42 PM
In my case an upgrade from 2011.1.315 to Q3 2011 (2011.3.1115) using Razor syntax generated this error. The fix was the same but with different details and syntax, only required adding a question mark to the end of line 1 of Views\Shared\EditorTemplates\Date.cshtml:

@model DateTime?
Tags
Grid
Asked by
Derek
Top achievements
Rank 1
Answers by
Frank
Top achievements
Rank 1
Atanas Korchev
Telerik team
Derek
Top achievements
Rank 1
David Whiteley
Top achievements
Rank 1
Jeremy
Top achievements
Rank 1
Share this question
or