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

How to Bind a Grid to a Property of an Object

1 Answer 290 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Randal
Top achievements
Rank 1
Randal asked on 15 Aug 2016, 12:40 PM

I am unable to create a new record, i.e. nothing pops up, when my grid is bound to my Person.Employer's EmployerName string property. 

@model IList<EmployeeManager.Web.EmployeeServiceRef.Person>
@(Html.Kendo().Grid(Model)
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.PersonName);
        columns.Bound(p => p.PhoneNumber);
        columns.Bound(p => p.Employer.EmployerName);
        columns.Bound(p => p.Address);
    })
    .ToolBar(toolbar => toolbar.Create())
    .Editable(editable => editable.Mode(GridEditMode.PopUp))

When I comment out p.Employer.EmployerName, the New button pops up a dialog as expected. Is there any way I can bind my Employee object to the grid so that I can edit these details as well? The plan is to create a dropdown list of the Employer Name's once I can figure out how to bind it to an Add New call.

Any help would be greatly appreciated.

1 Answer, 1 is accepted

Sort by
0
Randal
Top achievements
Rank 1
answered on 15 Aug 2016, 03:01 PM

I finally figured it out. I had to create a default value in my datasource so that the Employer object could be initialized.

 

.DataSource(dataSource => dataSource
    .Ajax()
    .PageSize(25)
    .Model(model =>
    {
        model.Id(p => p.Id);
        model.Field(p => p.Family).DefaultValue(new EmployeeManager.Web.UI.EmployeeServiceRef.Employer());
    })

Tags
Grid
Asked by
Randal
Top achievements
Rank 1
Answers by
Randal
Top achievements
Rank 1
Share this question
or