Marco Levarato
Marco Levarato

Reputation: 317

Bind a ModelView with a List of string on a KendoGrid, with the mvc Wrapper

Hi i have a ModelView as this

public class GridUser
            {
                    public long id { get; set; }
                    public string username { get; set; }
                    public string name { get; set; }
                    public string email { get; set; }
                    public string surname { get; set; }
                    public DateTime createDate { get; set; }
                    public List<string> customfields { get; set; }
            }

I cant bind the list of the string of the string writing a foreach inner the

.Columns(columns =>{

Please, someone can post a sample code with a bind of a list?

Upvotes: 1

Views: 3012

Answers (1)

Petur Subev
Petur Subev

Reputation: 20193

If you bind the column to this list property - you wont display that collection properly - neither you will be sort/filter etc. You can use a Template/ClientTemplate (depending on your binding) column to iterate through the collection and display the items. e.g.

@model IEnumerable<GridOneToManyTemplate.Models.Customer>

@(Html.Telerik().Grid<GridOneToManyTemplate.Models.Customer>()
    .DataBinding(config=> config.Ajax()
                            .Select("Customers","Home"))
    .Name("Grid")
    .Columns(cols =>
        { 
            cols.Bound(c => c.CompanyName);
            cols.Bound(c => c.Orders).ClientTemplate("<#= template(data) #>");
        })
    .Pageable(pages=> pages.PageSize(3))
)


<script>
    function template(item) {
        var html = "<ul>";
        for (var i = 0; i < item.Orders.length; i++) {
            html += "<li>";
            html += item.Orders[i].OrderID;
            html += "</li>";
        }
        html += "</ul>";
        return html;
    }  
</script>

Upvotes: 2

Related Questions