This is a migrated thread and some comments may be shown as answers.

[Solved] Telerik MVC 3 GridView Reusable view

0 Answers 88 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Michael
Top achievements
Rank 1
Michael asked on 02 Mar 2012, 10:22 AM
Hello,

For the sake of DRY principle, we would like to have only 1 view per object type and define which columns should be displayed by adding Expression<Func<T, object>> to a setting object that will hold the collection and the all settings for the table.

public class TelerikTableSettings<T> where T : IEORTCObject
    {
        public string Name { get; set; }
        public ICollection<T> Items { get; set; }
        public ICollection<Expression<Func<T, object>>> Columns {get;set;}
 
        public TelerikTableSettings()
        {
            this.Name = typeof(T).Name;
            this.Columns = new List<Expression<Func<T, object>>>();
        }
 
        public void AddColumn(Expression<Func<T, object>> property)
        {
            if (!this.Columns.Contains(property))
                this.Columns.Add(property);
        }
    }


So we use this item to hold every columns.
We add the columns in the controller :

public ActionResult Index()
        {
            TelerikTableSettings<Group> settings = new TelerikTableSettings<Group>();
            settings.Items = prismaManager.GroupManager.Select();
            settings.AddColumn(g => g.Name);
            settings.AddColumn(g => g);
            return View(settings);
        }


And in the view, we would like to be able to choose which columns should be displayed by iterating over the Columns expressions.

.Columns(columns =>
    {
        foreach(var col in Model.Columns)
        {
            columns.Bound(g => col.Compile().DynamicInvoke(g));
        }
    })


But this doesn't work.
Does anyone have any idea how I'm supposed to achieve that ?

Thanks.
Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Share this question
or