Binding and displaying information from complex C# objects

2 posts, 0 answers
  1. Dave
    Dave avatar
    29 posts
    Member since:
    Aug 2010

    Posted 11 Aug 2015 Link to this post

    Hello,

     If the Telerik controls do not support this, then I would like to know. Thanks in advance

     I have a Grid that binds to a ViewModel, that in turn has 2 other types of classes as its properties. I am trying to populate the grid rows with both, and either property could be Null. In one of these classes, there is a property that is a List<Skill> where each of the Skills are classes that has a string that I need to concatenate into a larger one to display for the 'Skill' grid column. Right now I'm just making the concatenated string in the controller and adding an extra property onto my ViewModal:

     columns.Bound(p => p.SkillsDisplayed).Title("Skill").ClientTemplate("<p>#= SkillsDisplayed ? SkillsDisplayed : '' #</p>");

     My question is: is there a way to put a Razor block in the ClientTemplate (or some other way in the column binding) to iterate through the List<Skill> and concatenate those object's strings together and display the resulting string? I have already tried this with javascript similar to the above, but the nested '#'s' makes such a function highly unreadable and I'm not sure I can achieve that anyways. Is this even possible?

  2. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2289 posts

    Posted 13 Aug 2015 Link to this post

    Hello Dave,

     

    The ClientTemplate must be a string which might contains client template. It will be evaluated on client when the widget is binding. The template might contain valid JavaScript. 

     

    That said you should be able to implement the client template in the same manner to concatenate the elements of array with JavaScript in similar manner as on server (using Razor).

     

    Example of such client template (assuming myField is either null or collection of strings):

     

    .ClientTemplate("#= myField == null ? 'N/A' : myField.join(', ') #")

     

     

    In browser console you can test the output of the above template as shown bellow:

     

      - kendo.template("#= myField == null ? 'N/A' : myField.join(', ') #")({myField: null})

    - kendo.template("#= myField == null ? 'N/A' : myField.join(', ') #")({myField: ["foo", "bar", "baz"]})

     

    Regards,
    Nikolay Rusev
    Telerik
    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