我想使用MVC网格过滤但不想使用linq。当前示例使用linq进行过滤。但是,我想将过滤选项传递给存储过程。你有一个例子吗?
我见过Telerik扩展网格的示例,其中内部控制器在使用GridActionAttribute和GridModel从数据库接收所有记录后过滤记录。
[GridAction]
public ActionResult _Paging()
{
return View(new GridModel<Order>
{
Data = GetOrders()
});
}
我希望在执行数据库查询之前使用此过滤器,分页信息。所以我将只从数据库中获取pagesize记录。不是整个数据库记录来过滤它(使用storedproc)。 另外,从数据库LINQ或ENTLIB DAL获取记录的最佳做法是什么。
答案 0 :(得分: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
}
我只是称这种方法对我来说很好。我还没有测试过datetime列。
答案 1 :(得分:0)
考虑使用自定义Ajax绑定。请查看此part of the documentation和this演示,了解相关信息。