Null reference when adding a new row to a grid.

3 posts, 0 answers
  1. Jim
    Jim avatar
    25 posts
    Member since:
    Mar 2016

    Posted 15 Apr Link to this post

    I am getting the error:  JavaScript runtime error: Unable to get property 'xxx' of undefined or null reference.

    This occurs when I click the "Add New Record" in my Kendo grid.

    Does this have something to do with the way the model in setup?

    I was able to add a record previously when my data record was more flattened out.

    Please advise.

     

    My grid is setup as:

    @(Html.Kendo().Grid<MyTest.Models.PersonModel>()
        .Name("myGrid")
        .Columns(col =>
        {
            col.Bound(x => x.Person.ID);
            col.Bound(x => x.Person.Field1);
            col.Bound(x => x.Person.Field2);
            col.Bound(x => x.Person.Field3);
            col.Command(x => {
                x.Custom("Extra Options").Click("showExtraOptions");
                x.Destroy();
            });
        })
        .ToolBar(x =>
        {
            x.Create();
            x.Save();
        })
        .Sortable()
        .Editable(x => x.Mode(GridEditMode.InCell))
        .Navigatable()
        .Scrollable(x => x.Height("auto"))
        .Filterable(x => x
            .Extra(false)
            .Mode(GridFilterMode.Menu)
            .Operators(op => op
                .ForString(str => str.Clear()
                    .Contains("Contains")
                    .StartsWith("Starts with")
                    .IsEqualTo("Is equal to")
                )
            )
        )
        .Pageable(p => p
                    .Refresh(true)
                    .PageSizes(true)
                    .ButtonCount(5))
        .DataSource(ds => ds
            .Ajax()
            .ServerOperation(false) // Paging, sorting, filtering and grouping will be done client-side
            .Batch(true)
            .PageSize(20)
            .Model(m =>
                {
                    m.Id(x => x.Person.ID);
                })
            .Read(r => r.Action("GetPerson", "Home"))
            .Create(x => x.Action("CreatePerson", "Home"))
            .Destroy(x => x.Action("DeletePerson", "Home"))
            .Update(x => x.Action("UpdatePerson", "Home"))
        )
    )

     

    My model is:

    public class PersonModel
    {
        public My_Person Person{ get; set; }
        public List<Person_Options> PersonOptions { get; set; }
    }
     
    public class My_Person
    {
        public string ID{ get; set; }
        public string Field1 { get; set; }
        public string Field2 { get; set; }
        public string Field3 { get; set; }
    }

     

  2. Alexander Popov
    Admin
    Alexander Popov avatar
    1416 posts

    Posted 19 Apr Link to this post

    Hello Jim,

    This happens because there are columns bound to complex object nested fields. Since there is no default value for those the Person field, it is initially undefined and trying to get the Field1 property of undefined throws an error. My advice is to specify a default value inside the DataSource's Model option.

    Regards,
    Alexander Popov
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Jim
    Jim avatar
    25 posts
    Member since:
    Mar 2016

    Posted 19 Apr in reply to Alexander Popov Link to this post

    Thank you Alexander.  Setting the default resolved my issue.
Back to Top