"The DataKeys collection is empty. Please specify a data key."

6 posts, 1 answers
  1. figueiredorj
    figueiredorj avatar
    68 posts
    Member since:
    Mar 2011

    Posted 10 Jun 2012 Link to this post

    Hi. 
    I am having an used (I think) issue on grid.
    "The DataKeys collection is empty. Please specify a data key."

    For telerik mvc extensions it is documented issue but I don't find how to fix it with kendo MVC
    My grid:
    @(Html.Kendo().Grid(Model)
     
        .Name("Grid")
        .Columns(columns =>
                     {
                          
                         columns.Bound(p => p.ClientSetName).Width(100).Title("Client");
                         columns.Bound(p => p.IMEI).Width(100).Title("IMEI");
                         columns.Bound(p => p.Active).Width(200).Title("Active");
                         columns.Bound(p => p.Inserted).Title("Inserted");
                         columns.Command(commands =>
                         {
                             commands.Edit();
                         }).Width(200);
                     })
     
            .Groupable()
            .Pageable()
            .Sortable()
            .Scrollable()
            .Filterable()
            .DataSource(dataSource => dataSource
                .Ajax()
                .Read(read => read.Action("_Index", "Kit"))
            )
    )
    and my ViewModel:
    public class KitViewModel
    {
        [ScaffoldColumn(true)]
        [UIHint("Kit")]
        public Guid Id { get; set; }
     
        [StringLength(15), Required]
        [DisplayName("IMEI")]
        public string IMEI { get; set; }
     
        [ReadOnly(true)]
        [DisplayName("Active")]
        public bool Active { get; set; }
     
        [DisplayFormat(DataFormatString = "{0:dd MM yyyy}")]
        [DataType(DataType.DateTime), Required]
        [ReadOnly(true)]
        [DisplayName("Inserted")]
        public DateTime Inserted { get; set; }
     
        [DisplayFormat(DataFormatString = "{0:dd MM yyyy}")]
        [DataType(DataType.DateTime), Required]
        [ReadOnly(true)]
        [DisplayName("Updated")]
        public DateTime Updated { get; set; }
     
        [UIHint("Client")]
        [ScaffoldColumn(true)]
        public Guid ClientId { get; set; }
     
        [DisplayName("Client")]
        public string ClientSetName { get; set; }
    }
    How can I fix this in kendo mvc?
    Thanks
  2. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 11 Jun 2012 Link to this post

    Hello,

    The error is raised due to the fact that no id field is specified. This should be set through DataSource's configuration, similar to the following:

    .DataSource(dataSource => dataSource       
            .Model(model => model.Id(p => p.Id))
            //....
    )

    Regards,
    Rosen
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. figueiredorj
    figueiredorj avatar
    68 posts
    Member since:
    Mar 2011

    Posted 11 Jun 2012 Link to this post

    Hi Rosen.
    Thanks for answering.

    However unless I am using an outdated dll for mvc kendo I do not have access Model in Datasource.
    Is there an updated dll other than the one in msi installer?

    Thanks
  5. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 11 Jun 2012 Link to this post

    Hello,

    Could you please provide a small sample in which the behavior you are experiencing can be observed locally. Meanwhile, you may refer to this help article which describes how to configure Grid widget for Ajax editing.

    Regards,
    Rosen
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  6. figueiredorj
    figueiredorj avatar
    68 posts
    Member since:
    Mar 2011

    Posted 11 Jun 2012 Link to this post

    Hi Again.
    This is my grid code:
    @(Html.Kendo().Grid( Model)
        .Name("Grid")
        .Columns(columns =>
                     {
                          
                         columns.Bound(p => p.ClientSetName).Width(100).Title("Client");
                         columns.Bound(p => p.Active).Width(200).Title("Active");
                         columns.Bound(p => p.Inserted).Title("Inserted");
                       })
     
            .Groupable()
            .Pageable()
            .Sortable()
            .Scrollable()
            .Filterable()
            .DataSource(dataSource => dataSource
                 
                .Ajax()
                .Read(read => read.Action("_Index", "Kit"))
            )
    )
    As you mentioned I was expecting do add it on datasource... it would seem something like 

    .DataSource(dataSource => dataSource
        .Model(model => model.Id(p => p.Id))
        .Ajax()
        .Read(read => read.Action("_Index", "Kit"))
    )
    However I am unable to add Model as there is no definition for it.

    I am using it with a MVC4 project. Don't know if it matters. But I have tested on "Examples" of Kendo MVC to see if Model was there and behavior is the same.
  7. Answer
    Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 11 Jun 2012 Link to this post

    Hi,

    You should add it after the Ajax setting (as described in the article I have mentioned in my previous message):

    .DataSource(dataSource => dataSource
        .Ajax()
        .Model(model => model.Id(p => p.Id))
        .Read(read => read.Action("_Index", "Kit"))

    The Ajax setting is the "entry point" for this DataSource option.

    Regards,
    Rosen
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top
UI for ASP.NET MVC is VS 2017 Ready