sub objects cause not reaching the controller

4 posts, 0 answers
  1. Robert
    Robert avatar
    37 posts
    Member since:
    Aug 2015

    Posted 27 Jul Link to this post

    Currently we have complicated model which has objects.

     

    Model:

    class Person {
    string name{get; set;}
    ContactInfo contactInfo{get; set;}
    }

    class ContactInfo  {
    string phoneNumber {get; set;}
    string address{get; set;}
    }

     

    View:

     @(Html.Kendo().Grid(Model.Person).Name("testGrid")
    .Columns(columns =>
    {
     columns.Bound(x => x.name);
    columns.Bound(x => x.ContactInfo.phoneNumber);
    ....

     

     

    in JS Console, with "$("#testGrid").data("kendoGrid").dataSource.data()" I see everything is good at the first load.

     

    However, when I edit "phonenumber", the datasource looks like this:

    o
    -> Person 
         ->Name:"xxx"
         ->ContactInfo
                ->phoneNumber :"xxxx"
                 ->address : "xxxxxx"
         ->ContactInfo.phoneNumber: ""

     

    we used Jquery.ajax to send the data in js so it has duplicated "ContactInfo.phoneNumber" after stringify, then it never reaches the MVC controller.

     

    Questions:
    1. How to solve this issue?
    2. How to avoid this additional "ContactInfo.phoneNumber" been created?

    Thanks!

  2. Vasil
    Admin
    Vasil avatar
    1547 posts

    Posted 29 Jul Link to this post

    Hi Robert,

    The grid and its datasource are designed to bind to flat data only.

    To make this work correctly you may want to add new model that combines all properties inside it. Something like:
    class PersonCustomModel {
    string name{get; set;}
    string phoneNumber {get{return contactInfo.phoneNumber}; set{contactInfo.phoneNumber = value};}
    string address{get{return contactInfo.address}; set{contactInfo.address = value};}
     
    ContactInfo contactInfo{get; set;}
    }

    Then you can bind your columns directly to the "phoneNumber" same way as for the "name"

    Regards,
    Vasil
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Robert
    Robert avatar
    37 posts
    Member since:
    Aug 2015

    Posted 29 Jul in reply to Vasil Link to this post

    Yes. that's the way we are doing by flatten out the objects. However, we have more than one layer and webapi. This way makes things complicated with a lot of the same mapping. In addition, we have way more fields than this samples which we try to avoid these kind of mapping manually. The client also requests the clean code without doing all those.

    May I know if dot property is always empty? if so, we can write a JS method hack to delete all dot properties before sending.

    thanks

     

     

  5. Vasil
    Admin
    Vasil avatar
    1547 posts

    Posted 02 Aug Link to this post

    Hello Robert,

    You could probably delete the excessive properties. But it is not guaranteed that it will work in wall cases, because it is not meant to be used that way.

    Regards,
    Vasil
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET MVC is VS 2017 Ready