This question is locked. New answers and comments are not allowed.
I'm trying to set the initial sorting of an MVC Grid using property names passed through the Model. Here's how my model looks:
The problem is that I can't figure out how to get the grid initial sorting to work. Here's what I've tried:
You can see I've tried two methods of reflection to get the property instance. In the grouping, I tried retrieving the property by name by a typeof(ItemsViewModel) call. In sorting, I tried using the class instance passed in the lambda expression (c.GetType()). Neither work.
Sorting/grouping after that initial rendering work, so it's not a problem with that core functionality.
Is there any way to set the initial sorting and grouping by using the property name only?
public class RenderViewModel{ public RenderViewModel() { VisibleProperties = new List<string>(); GroupByProperties = new List<string>(); Orders = new List<RenderViewOrderByModel>(); } public string Name { get; set; } public int Id { get; set; } public List<string> VisibleProperties { get; set; } public int PageSize { get; set; } public List<string> GroupByProperties { get; set; } public List<RenderViewOrderByModel> Orders { get; set; }}public class RenderViewOrderByModel{ public string PropertyName { get; set; } public bool Ascending { get; set; }}The problem is that I can't figure out how to get the grid initial sorting to work. Here's what I've tried:
<div class="body corners noTR noTL"> @(Html.Telerik().Grid<ItemsViewModel>() .Name("ItemsGrid") .Columns(columns => { columns.Bound(x => x.Id) .Visible(!Model.VisibleProperties.Any() || Model.VisibleProperties.Contains("Id")) .Title("Number") .Width(50) .HeaderHtmlAttributes(new { @style = "text-align: center;" }) .HtmlAttributes(new { @style = "text-align: center;" }); columns.Bound(x => x.Name) .Visible(!Model.VisibleProperties.Any() || Model.VisibleProperties.Contains("Name")) .Title("Name"); }) .Groupable(grouping => grouping.Groups(groups => Model.GroupByProperties.ForEach(x => groups.Add(c => typeof(ItemsViewModel).GetProperty(x))))) .DataBinding(db => db.Ajax().Select("RenderItems", "Views", new { id = Model.Id })) .Pageable(pager => pager.PageSize(Model.PageSize)) .Sortable(sorting => sorting.SortMode(GridSortMode.MultipleColumn) .OrderBy(ordering => { Model.Orders.Where(x => x.Ascending).ToList().ForEach(x => ordering.Add(c => c.GetType().GetProperty(x.PropertyName)).Ascending()); Model.Orders.Where(x => !x.Ascending).ToList().ForEach(x => ordering.Add(c => c.GetType().GetProperty(x.PropertyName)).Descending()); })) .ClientEvents(events => events.OnDataBound("grid_onDataBound")) )</div>You can see I've tried two methods of reflection to get the property instance. In the grouping, I tried retrieving the property by name by a typeof(ItemsViewModel) call. In sorting, I tried using the class instance passed in the lambda expression (c.GetType()). Neither work.
Sorting/grouping after that initial rendering work, so it's not a problem with that core functionality.
Is there any way to set the initial sorting and grouping by using the property name only?