Repeater with list of custom object

Tomasz Czado asked on August 24, 2017 14:21

Hi,

I have custom web part with cms:CMSRepeater. In code behind I'm binding a list of custom objects to this repeater. Object looks like this one:

public class MediaPost
{
    public string PostID { get; set; }
    public string Post { get; set; }
    public string PostDate { get; set; }
    public string ImageURL { get; set; }
    public int LikeCount { get; set; }
    public string LinkUrl { get; set; }
}

I have transformation Text/XML where I'm trying to display for example {% PostDate %} - but nothing is showing. How to display properties from custom

Correct Answer

Mike Wills answered on August 28, 2017 20:37

Hi Tomasz,

I was able to bind a repeater to custom data objects, by making sure the custom objects implemented IDataContainer, as described here:

https://www.kenticotricks.com/blog/using-the-applytransformation-macro

Mike

0 votesVote for this answer Unmark Correct answer

Recent Answers


Trevor Fayas answered on August 24, 2017 14:33

usually a repeater data source is a data table, have you tried converting your list of objects to a data table with the columns matching the field names?

0 votesVote for this answer Mark as a Correct answer

Tomasz Czado answered on August 24, 2017 14:34

No, because datas comes from external API and I wanted to bind it to repeater and use transformation to control how each item is displayed.

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on August 24, 2017 15:32

Download the universal api viewer with hierarchy support from the marketplace, in there you'll find how i do exactly what you are doing, converting a strongly typed api class to a repeater to transform. Already did that work for you and wrote the classes that take a list of .net objects and converts it into a proper transformable object. That's why i created that webpart.

http://devtrev.com/Resources/Universal-Api-Viewer

https://devnet.kentico.com/marketplace/utilities/universal-api-viewer-(with-hierarchy-support)

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on August 24, 2017 15:51 (last edited on August 24, 2017 15:54)

I think your transformation type is not set to Text/XML and because of that it doesn't not resolve macros : Image Text

If it is still does not work try convert it to DataTable. DataTable will work for sure.

public static DataTable ToDataTable<T>(List<T> items)
{
        DataTable dataTable = new DataTable(typeof(T).Name);

        //Get all the properties
        PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
        foreach (PropertyInfo prop in Props)
        {
            //Defining type of data column gives proper data table 
            var type = (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) ? Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType);
            //Setting column names as Property names
            dataTable.Columns.Add(prop.Name, type);
        }
        foreach (T item in items)
        {
           var values = new object[Props.Length];
           for (int i = 0; i < Props.Length; i++)
           {
                //inserting property values to datatable rows
                values[i] = Props[i].GetValue(item, null);
           }
           dataTable.Rows.Add(values);
      }
      //put a breakpoint here and check datatable
      return dataTable;
}
1 votesVote for this answer Mark as a Correct answer

rahul gaur answered on April 18, 2018 08:05

Hey guys having any problem in getting free robux then try our link and enjoy gaming as soon as possible

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.