2

我想使用 MVC 网格过滤,但不想使用 linq。当前示例使用 linq 进行过滤。但是,我想将过滤选项传递给存储过程。你有一个例子吗?

我已经看到 Telerik 扩展网格的示例,它在控制器内部使用 GridActionAttribute 和 GridModel 从数据库接收所有记录后过滤记录。

[GridAction]
    public ActionResult _Paging()
    {
        return View(new GridModel<Order>
        {
            Data = GetOrders()
        });
    }

我想要这个过滤器,在执行数据库查询之前分页信息。这样我就只能从数据库中获取 pagesize 记录。不是整个数据库记录来过滤它(使用storedproc)。另外,从数据库 LINQ 或 ENTLIB DAL 获取记录的最佳实践是什么。

4

2 回答 2

11

我得到了解决方案。这是我的助手类,它为我构建过滤器查询。

using Telerik.Web.Mvc;

/// <summary>
/// TODO: Update summary.
/// </summary>
public class GridBinder
{
    public int PageNumber
    {
        get { return _pageNumber; }
        set { _pageNumber = value; }
    }

    public int PageSize
    {
        get { return _pageSize; }
        set { _pageSize = value; }
    }

    public int RecordCount { get; set; }

    public SortInfo SortInfo
    {
        get { return _sortInfo; }
        set { _sortInfo = value; }
    }

    private readonly GridCommand _command;
    private int _pageNumber = 1;
    private int _pageSize = 10;
    private SortInfo _sortInfo = new SortInfo() { Direction = SortDirection.Asc, Member = string.Empty };

    public GridBinder(GridCommand command)
    {
        this._command = command;
        GetSortDescriptor();
    }

    private void GetSortDescriptor()
    {
        SortInfo sortInfo;
        foreach (SortDescriptor descriptor in this._command.SortDescriptors)
        {
            this.SortInfo.Member = descriptor.Member;
            this.SortInfo.Direction = descriptor.SortDirection == ListSortDirection.Ascending ? SortDirection.Asc : SortDirection.Desc;
        }
    }

    public string GetFilterDescriptor()
    {
        string filters = string.Empty;
        foreach (IFilterDescriptor filter in this._command.FilterDescriptors)
        {
            filters += ApplyFilter(filter);
        }

        return filters;
    }

    private static string ApplyFilter(IFilterDescriptor filter)
    {
        var filters = string.Empty;
        if (filter is CompositeFilterDescriptor)
        {
            filters += "(";
            var compositeFilterDescriptor = (CompositeFilterDescriptor)filter;
            foreach (IFilterDescriptor childFilter in compositeFilterDescriptor.FilterDescriptors)
            {
                filters += ApplyFilter(childFilter);
                filters += " " + compositeFilterDescriptor.LogicalOperator.ToString() + " ";
            }
        }
        else
        {
            string filterDescriptor = "{0} {1} {2}";
            var descriptor = (FilterDescriptor)filter;
            if (descriptor.Operator == FilterOperator.StartsWith)
            {
                filterDescriptor = string.Format(filterDescriptor, descriptor.Member, "LIKE", "'" + descriptor.Value + "%'");
            }
            else if (descriptor.Operator == FilterOperator.EndsWith)
            {
                filterDescriptor = string.Format(filterDescriptor, descriptor.Member, "LIKE", "'%" + descriptor.Value + "'");
            }
            else if (descriptor.Operator == FilterOperator.Contains)
            {
                filterDescriptor = string.Format(filterDescriptor, descriptor.Member, "LIKE", "'%" + descriptor.Value + "%'");
            }
            else if (descriptor.Operator == FilterOperator.DoesNotContain)
            {
                filterDescriptor = string.Format(filterDescriptor, descriptor.Member, "NOT LIKE", "'%" + descriptor.Value + "%'");
            }
            else if (descriptor.Operator == FilterOperator.IsEqualTo)
            {
                filterDescriptor = string.Format(filterDescriptor, descriptor.Member, "=", "'" + descriptor.Value + "'");
            }
            else if (descriptor.Operator == FilterOperator.IsNotEqualTo)
            {
                filterDescriptor = string.Format(filterDescriptor, descriptor.Member, "<>", "'" + descriptor.Value + "'");
            }
            else if (descriptor.Operator == FilterOperator.IsGreaterThan)
            {
                filterDescriptor = string.Format(filterDescriptor, descriptor.Member, ">", "'" + descriptor.Value + "'");
            }
            else if (descriptor.Operator == FilterOperator.IsGreaterThanOrEqualTo)
            {
                filterDescriptor = string.Format(filterDescriptor, descriptor.Member, ">=", "'" + descriptor.Value + "'");
            }
            else if (descriptor.Operator == FilterOperator.IsLessThan)
            {
                filterDescriptor = string.Format(filterDescriptor, descriptor.Member, "<", "'" + descriptor.Value + "'");
            }
            else if (descriptor.Operator == FilterOperator.IsLessThanOrEqualTo)
            {
                filterDescriptor = string.Format(filterDescriptor, descriptor.Member, "<=", "'" + descriptor.Value + "'");
            }

            filters = filterDescriptor;
        }

        filters = filters.EndsWith("And ") == true ? filters.Substring(0, filters.Length - 4) + ")" : filters;
        filters = filters.EndsWith("Or ") == true ? filters.Substring(0, filters.Length - 4) + ")" : filters;

        return filters;
    }
}
public class SortInfo
{
    public string Member { get; set; }
    public SortDirection Direction { get; set; }
}

public enum SortDirection
{
    Asc, Desc
}

我只是称这种方法对我来说很好用。我还没有测试过日期时间列。

于 2012-12-14T07:18:05.993 回答
0

考虑使用自定义 Ajax 绑定。检查这部分文档这个演示以获得这个想法。

于 2012-12-07T13:14:59.360 回答