One to One relation

4 posts, 0 answers
  1. Julien
    Julien avatar
    2 posts
    Member since:
    Jan 2020

    Posted 31 Jan 2020 Link to this post

    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.

  2. Preslav
    Admin
    Preslav avatar
    591 posts

    Posted 05 Feb 2020 Link to this post

    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.
  3. Julien
    Julien avatar
    2 posts
    Member since:
    Jan 2020

    Posted 06 Feb 2020 in reply to Preslav Link to this post

    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.

  4. Preslav
    Admin
    Preslav avatar
    591 posts

    Posted 11 Feb 2020 Link to this post

    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.
Back to Top