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

One to One relation

3 Answers 50 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Julien
Top achievements
Rank 1
Julien asked on 31 Jan 2020, 03:55 PM

Hi,

I have a class with more than 60 fields. Our quality gate doesn't allow more than 20 fields. So I factorized my code and have some class with one to one relations.

Is there something that I can use to have only one grid with all my fields?

I tried something like this, and got an error data.AppFacts is undefined :

@(Html.Kendo().Grid<App>()
      .Name("grid")
      .Columns(columns =>
          {
              columns.Bound(a => a.Name);
              columns.Bound(a => a.AppData.DatabaseInstances);
              columns.Bound(a => a.AppFacts.ApplicationType);
          }
    )

I tried with columns.ForeignKey, but I don't think I have understand how it work.

I'm a new developer, so it might be obvious but I tried.

3 Answers, 1 is accepted

Sort by
0
Preslav
Telerik team
answered on 05 Feb 2020, 01:21 PM

Hi Julien,

The provided code looks good. If the App model has a property AppFacts, and this property is of a class type that has ApplicationType property in it, everything should work as expected.

For example, I tested the following scenario:

namespace Kendo.Mvc.Examples.Models
{
    public class ProductViewModel
    {
        //...

        public CategoryViewModel Category
        {
            get;
            set;
        }
        
        //...
    }
}

//.............

namespace Kendo.Mvc.Examples.Models
{
	public class CategoryViewModel
    {
        public int CategoryID { get; set; }
        public string CategoryName { get; set; }
    }
}

//..................

Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(p => p.Category.CategoryName);
        })
//....

The above code resulted in the following Grid: https://www.screencast.com/t/amgnTsdBOZ

 

Regards,
Preslav
Progress Telerik

Get quickly onboarded and successful with Telerik UI for ASP.NET Core with the dedicated Virtual Classroom technical training, available to all active customers.
0
Julien
Top achievements
Rank 1
answered on 06 Feb 2020, 09:05 AM

Thanks for the reply,

I've got the same answer from a colleague and it's work fine for reading.

My problem occur when I want to create a new object.

In your exemple, when you create a new row, you might have an error because your CategoryViewModel is not defined.

0
Preslav
Telerik team
answered on 11 Feb 2020, 08:58 AM

Hello Julien,

To create a new object correctly, assign a default value to the field in the model of the DataSource.

For example, check the code of this demo - https://demos.telerik.com/aspnet-core/grid/editing-custom

 

Regards,
Preslav
Progress Telerik

Get quickly onboarded and successful with Telerik UI for ASP.NET Core with the dedicated Virtual Classroom technical training, available to all active customers.
Tags
Grid
Asked by
Julien
Top achievements
Rank 1
Answers by
Preslav
Telerik team
Julien
Top achievements
Rank 1
Share this question
or