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

Issue with DateTime fields being Null

4 Answers 561 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chad Johnson
Top achievements
Rank 1
Chad Johnson asked on 11 Sep 2012, 03:11 PM
Greetings,

I'm currently in the process of creating a grid to display comments on a product.  There is some data migration that has forced two DateTime fields to be required Null and the grid seems to be throwing fits about it.  I get the following error when it happens:

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

I have been researching the topic and had little success on solving the problem.  Here is a sample of my code thus far.

View

@(Html.Telerik().Grid<ProductModel.ProductCommentsModel>()
                        .Name("productcomments-grid")
                        .DataKeys(keys =>
                        {
                            keys.Add(x => x.Id);
                        })
                        .DataBinding(dataBinding =>
                        {
                            dataBinding.Ajax()
                                .Select("ProductCommentsList", "Product", new { productId = Model.Id })
                                .Update("ProductCommentUpdate", "Product")
                                .Delete("ProductCommentDelete", "Product");
                        })
                        .Columns(columns =>
                        {
                            columns.Bound(x => x.Id)
                                .Hidden(true);
                            columns.Bound(x => x.ProductId)
                                .Hidden(true);
                            columns.Bound(x => x.CustomerId)
                                .Hidden(true);
                            columns.Bound(x => x.CustomerName)
                                .Width(200)
                                .ReadOnly(true);
                            columns.Bound(x => x.CreatedDate)
                                .Width(100)
                                .ReadOnly(true);
                            columns.Bound(x => x.LastModifiedDate)
                                .Width(100)
                                .ReadOnly(true);
                            columns.Bound(x => x.Comment);
                            columns.Command(commands =>
                            {
                                commands.Edit().ButtonType(GridButtonType.Text);
                                commands.Delete().ButtonType(GridButtonType.Text);
                            });
                        })
                        .Editable(edit => edit.Mode(GridEditMode.PopUp))
                        .EnableCustomBinding(true))

Model
public partial class ProductCommentsModel : BaseNopEntityModel
        {
            public int ProductId { get; set; }
  
            public int CustomerId { get; set; }
  
            public string CustomerName { get; set; }
  
            [Required(AllowEmptyStrings=true)]
            [DisplayFormat(NullDisplayText = "", DataFormatString = "0:MM/dd/yyyy")]
            public DateTime? CreatedDate { get; set; }
  
            [Required(AllowEmptyStrings = true)]
            [DisplayFormat(NullDisplayText = "", DataFormatString = "0:MM/dd/yyyy")]
            public DateTime? LastModifiedDate { get; set; }
  
            public DateTime? SentForRepair { get; set; }
  
            public string Comment { get; set; }
        }

Any help would be appreciated.  Thank you.

Kindest Regards,
Chad Johnson

4 Answers, 1 is accepted

Sort by
0
Simon
Top achievements
Rank 1
answered on 23 Oct 2012, 09:25 AM
Same problem here, any luck fixing it?
0
Chad Johnson
Top achievements
Rank 1
answered on 23 Oct 2012, 11:55 AM
I ended up loading the two datetime fields into strings instead.  It was my only course of action since I have not heard anything from Kendo or found any other sources that had it fixed.
0
Accepted
Simon
Top achievements
Rank 1
answered on 23 Oct 2012, 12:29 PM
Hey Chad,

Just found the problem, In you solution go to Views-->Shared and Create a new folder called EditorTemplates if it is not already there

Then Create these two Razor Views (or the web form equivalent as partial .ascx views)

Date.cshtml

@model DateTime?
 
@(Html.Kendo().DatePickerFor(m => m))

DateTime.cshtml

@model DateTime?
 
@(Html.Kendo().DateTimePickerFor(m => m))



If you already have these files make sure the Model is of type DateTime? and not DateTime

Worked for me, more information on editor templates can be found here

http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/editor-templates


Hope this helps,

Si
0
Chad Johnson
Top achievements
Rank 1
answered on 23 Oct 2012, 01:00 PM
Thanks.  I had seen this before but it was easier in the end for me just to convert them to strings.  I spoke with a person who has a firm understanding of MVC3 and 4, who said probably using strings would be the better course for the moment.
Tags
Grid
Asked by
Chad Johnson
Top achievements
Rank 1
Answers by
Simon
Top achievements
Rank 1
Chad Johnson
Top achievements
Rank 1
Share this question
or