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

row selection does not work on ef datamodel with navigation properties.

1 Answer 141 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Liza
Top achievements
Rank 1
Liza asked on 29 Oct 2012, 09:43 AM
When i set the property .Selectable() on a grid which is bound to a datamodel that has a parent model i get an error. 
It looks like its trying to get the parent datamodel but it cant because the object context is closed.

 here is my code 
 
@using Kendo.Mvc.UI
   
@{
 
     var addressList = Model.DataList as List<Intelli.Data.Parties.EFDAL.Entity.GeoAddress>;
  
  }
 
    @(Html.Kendo().Grid(addressList)
    .Name("GridPartyGeoAddresses")
    .Columns(columns =>
            {
                columns.Bound(p => p.ID).Title("ID").Hidden(true);
                columns.Bound(p => p.Address).Title("Address").Width(160);
                columns.Bound(p => p.Region).Title("Region").Width(80);
 
                columns.Bound(p => p.Zipcode).Title("Zipcode").Width(80);
                columns.Command(commands => commands
                                                .Custom("btnviewgeoaddress")
                                                .Text("View")
                                                .Click("viewgeoaddress")
                                                .HtmlAttributes(new { style = "text-align: center" }));
                columns.Command(commands => commands
                                                .Custom("btndeletegeoaddress")
                                                .Text("Remove")
                                                .Click("deletegeoaddress")
                                                .HtmlAttributes(new { style = "text-align: center" }));               
                 
 
            })
 
    .Scrollable(scr => scr.Enabled(true).Height(300))
    .Sortable(builder => builder.Enabled(true))
    .Resizable(resizing => resizing.Columns(true))
    .Selectable()
                    .DataSource(dataSource => dataSource
                                                  .Ajax()
                                                   .ServerOperation(false)
 
                        )
     
    )

 
GeoAddress datamodel has a parent model 'Person' in the ef model.

the error i get is : 
 'The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.'

If i comment out these properties : 

.Selectable()
  .DataSource(dataSource => dataSource
                         .Ajax()
                         .ServerOperation(false)
                        )


Then it works.
It looks like that setting selectable tries to call all model properties (and if one of those is lazy loading data from db error occurs). 
I also have no problem on other models that do not have parent associations with other data models.





1 Answer, 1 is accepted

Sort by
0
Accepted
Vesselin Obreshkov
Top achievements
Rank 2
answered on 29 Oct 2012, 07:06 PM
You generally shouldn't use your EF models directly with your UI but rather use flattened ViewModels. Then you won't run into this problem. Look at AutoMapper - we use it a lot and it saves us a ton of time and coding. Keep in mind though if/when you start using their IQueryable.ToDataSourceResult() extension, you have to make sure your property names are the same between your EF and View models.
Tags
Grid
Asked by
Liza
Top achievements
Rank 1
Answers by
Vesselin Obreshkov
Top achievements
Rank 2
Share this question
or