public static async Task <IActionResult> ToDataSourceJsonResultAsync <T>(this IQueryable <T> source, GridCriteria criteria) where T : class { if (criteria.PostFilters != null && criteria.PostFilters.Count > 0) { foreach (var item in criteria.PostFilters) { source = source.Where(item.Field, item.Value, item.Operator); } } var count = await source.CountAsync(); if (!string.IsNullOrEmpty(criteria.SortBy)) { source = source.OrderBy(criteria.SortBy, criteria.SortDirection); } List <T> items; if (criteria.PaginationEnabled) { items = await source.Skip(criteria.Offset).Take(criteria.Limit).ToListAsync(); } else { items = await source.ToListAsync(); } return(new JsonResult(new { TotalCount = count, Data = items })); }
public static IHtmlContent DataGrid(this IHtmlHelper htmlHelper, string name, string dataSourceUrl, List <GridColumn> columns, GridCriteria criteria = null) { criteria = criteria ?? new GridCriteria(); var htmlBuilder = new HtmlContentBuilder(); htmlBuilder.AppendHtmlLine("<script>"); htmlBuilder.AppendHtmlLine("$(function(){"); htmlBuilder.AppendHtmlLine($"$('#{name}').dataGridBind('{dataSourceUrl}',{JsonConvert.SerializeObject(criteria)},{JsonConvert.SerializeObject(columns)});"); htmlBuilder.AppendHtmlLine("});"); htmlBuilder.AppendHtmlLine("</script>"); return(htmlBuilder); }