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

[Solved] Bind to column value in nested object?

7 Answers 55 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Cam
Top achievements
Rank 1
Cam asked on 10 Oct 2011, 08:29 PM
I would like to bind to a nested object in my model, but I get an "Invalid Column Name" from Entity Framework.
I'm really surprise since I can do something like @Model.NestedObject.name in straight MVC3/Razor easily.
It works fine if I just use the Ids (the foreign keys). 
Ideas?

View Code:

@Html.Telerik().Grid(Model).Name("MappingGrid").Columns(columns=>
    {
        columns.Bound(e=>e.VendorId);
        columns.Bound(e=>e.ProductId);
        columns.Bound(e=>e.EdiColumn.EdiColumnName);
        columns.Bound(e=>e.EdiColumn.EdiColumnName);
        columns.Bound(e=>e.EdiCanonical.CanonicalName);
        columns.Bound(e=>e.Description);
    }).Pageable(paging=>paging.PageSize(50)).Sortable().Filterable()



Given Models:
public class EdiCanonical
{
    [ScaffoldColumn(false)]
    [Key]
    public int CanonicalId { get; set; }
    [DisplayName("Canonical Name")]
    public string CanonicalName { get; set; }
 
}
 
public class EdiColumn
{
    [Key]
    [ScaffoldColumn(false)]
    public int EdiColumnId { get; set; }
    [DisplayName("ColumnName")]
    public string EdiColumnName  { get; set; }
    public string Description { get; set; }
 
}
public class EdiColumnCanonical
{
    [Key]
    [ScaffoldColumn(false)]
    public int EdiColumnCanonicalId { get; set; }
    [DisplayName("Vendor")]
    public int VendorId { get; set; }
    [DisplayName("Product")]
    public int ProductId { get; set; }
    [DisplayName("Column")]
    public virtual EdiColumn EdiColumn { get; set; }
    [DisplayName("Canonical")]
    public virtual EdiCanonical EdiCanonical { get; set; }
    [DisplayName("Description")]
    public string Description {get;set;}
}

7 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 12 Oct 2011, 01:13 PM
Hello Cam,

This is a very strange problem as the grid fully supports binding to nested object properties. In this demo for example we have the following column definition:

 columns.Bound(o => o.Customer.ContactName).Width(200);
When is the "Invalid Column Name" error generated? Can you show how your models look like? 

Regards,
Atanas Korchev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Cam
Top achievements
Rank 1
answered on 12 Oct 2011, 01:42 PM
See the models in the question above.

The error occurs right when generating the view.

Thanks!
Cam
0
Atanas Korchev
Telerik team
answered on 12 Oct 2011, 02:21 PM
Hi Cam,

 We would probably need the underlying database tables in order to reproduce the exact error message. Is there a chance to provide it as well? We can easily make a sample project using dummy data but the error won't appear for obvious reasons.

Regards,
Atanas Korchev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Cam
Top achievements
Rank 1
answered on 12 Oct 2011, 03:04 PM
Sure, I appreciate the help.  It's relatively simple.

Please find attached.

Thanks!

Cam
0
Atanas Korchev
Telerik team
answered on 12 Oct 2011, 04:07 PM
Hello Cam,

Are you using entity framework code first? If yes please send us the mapping configuration as well.

Regards,
Atanas Korchev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
Cam
Top achievements
Rank 1
answered on 12 Oct 2011, 04:21 PM
It is EF, I'm not sure it's using CodeFirst, but I'm using an existing database. 
I have included the DbContext/Entities class if that's what you're asking for.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace EdiAdmin.Models
{
    public class EdiEntities:DbContext
 
    {
        public DbSet<EdiCanonical> EdiCanonicals { get; set; }
        public DbSet<EdiColumn> EdiColumns { get; set; }
        public DbSet<EdiColumnCanonical> EdiColumnCanonicals{ get; set; }
    }
}
0
Atanas Korchev
Telerik team
answered on 12 Oct 2011, 04:34 PM
Hello Cam,

 I think the problem is not caused by Telerik Grid for ASP.NET MVC. It is rather caused by incomplete mapping configuration. I managed to get the same exception without using the grid at all. I just forced execution of the query:

var model = new EdiDbContext().EdiColumnCanonical;


model.ToList();

Find attached my test project.

In order to avoid this exception you need to configure the nested object mappings. Please refer to the Entity Framework documentation for instructions.

Greetings,
Atanas Korchev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
Tags
Grid
Asked by
Cam
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Cam
Top achievements
Rank 1
Share this question
or