用于MVC 3应用的分页的PaginatedList?错误:有一些无效的参数

时间:2013-05-31 18:12:16

标签: asp.net-mvc-3 datatable pagination

我有以下代码,我可以解释为什么它的无效参数:

 AuditDAL ad = new AuditDAL();
 var agencies = ad.SearchAgencies("Ak001", "");

string col = param.sColumns.Split(',')[param.iSortCol_0];
string orderby = col + " " + param.sSortDir_0;

//'AMS.Helper.PaginatedList.PaginatedList(System.Linq.IQueryable,int,int)'的最佳重载方法匹配包含一些无效参数C:\ NexGen \ AMS \ DEV \ Source \ AMS \ Controllers \ AuditController.cs

var qry = new PaginatedList<AuditAgency>(agencies, param.iDisplayStart, param.iDisplayLength);

PaginatedList代码:

namespace AMS.Helper
{
    public class PaginatedList<T> : List<T> {

        public int PageIndex  { get; private set; }
        public int PageSize   { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }

        public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize) {
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = source.Count();
            TotalPages = (int) Math.Ceiling(TotalCount / (double)PageSize);

            this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
        }

        public bool HasPreviousPage {
            get {
                return (PageIndex > 0);
            }
        }

        public bool HasNextPage {
            get {
                return (PageIndex+1 < TotalPages);
            }
        }
    }
}

搜索机构代码:

public IEnumerable<AuditAgency> SearchAgencies(string ori, string name)
        {
            List<AuditAgency> agencies = new List<AuditAgency>();
            using (var conn = new SqlConnection(_connectionString))
            {
                var com = new SqlCommand();
                com.Connection = conn;
                com.CommandType = CommandType.StoredProcedure;
                string term = "Ori";

                if (!String.IsNullOrEmpty(ori))
                {
                    term = "Ori";
                    com.Parameters.Add(new SqlParameter
                    {
                        ParameterName = "@ORI",
                        Value = ori
                    });
                }
                if (!String.IsNullOrEmpty(name))
                {
                    term = "legal_name";
                    com.Parameters.Add(new SqlParameter
                    {
                        ParameterName = "@Name",
                        Value = name
                    });
                }
                com.CommandText = "Audit_Get_Agency_List";
                var adapt = new SqlDataAdapter();
                adapt.SelectCommand = com;
                var dataset = new DataSet();
                adapt.Fill(dataset);

                agencies = (from c in dataset.Tables[0].AsEnumerable()
                            select new AuditAgency()
                            {
                                Agency_ID = Convert.ToInt32(c["Agency_Id"]),
                                Agency_Name = c["Agency_Name"].ToString(),
                                Agency_Ori = c["ORI"].ToString(),
                                COPSAuditNumber = c["COPSAuditNumber"].ToString(),
                                OIGAuditNumber = c["OIGAuditNumber"].ToString()
                            }).ToList<AuditAgency>();

                return agencies;
            }

        }

1 个答案:

答案 0 :(得分:0)

错误应该告诉你从哪里开始。

如果您启动调试器,我认为您会发现代理商是IEnumberable,但不是IQueryable

通过将SearchAgencies的返回类型从IQueryable更改为IEnumerable

来更正它

或者,您可以更改PaginatedList的类型以接受IEnumberables而不是IQueryables。这可能更安全,因为IQueryable继承自IEnumerable

(见 http://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspxDifferences between IQueryable, List, IEnumerator? 对于两者之间的区别)