Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

c# - Asp.net core pagination not working on datatable stored procedure

I get this error:

An unhandled exception occurred while processing the request.
NotImplementedException: The method or operation is not implemented.
Portal.Models.PaginatedList.CreateAsync(object p, int v, int pageSize) in PaginatedList.cs, line 47

Controller:

public async Task<IActionResult> Index(string sortOrder,
                                       string currentFilter,
                                       string searchString,
                                       int? pageNumber)
{
    List<StudentViewModal> listOfStudents = new List<StudentViewModal>();

    DataTable dataTable = new DataTable();

    using (SqlConnection sqlConnection = new SqlConnection(_configuration.GetConnectionString("SCHOOL")))
    {
        sqlConnection.Open();

        SqlDataAdapter sqlDa = new SqlDataAdapter("proc_GetStudents", sqlConnection);
        sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
        sqlDa.Fill(dataTable);

        foreach (DataRow dr in dataTable.Rows)
        {
            listOfStudents.Add(
                new StudentViewModal
                {
                    ID = Convert.ToInt32(dr["ID"]),
                    Name = Convert.ToString(dr["Name"]),
                });
        }
    }

    int pageSize = 3;

    return View(await PaginatedList<StudentViewModal>.CreateAsync(listOfStudents, pageNumber ?? 1, pageSize));
}

Here is the pagination helper class:

namespace Portal.Models.Referral
{
    public class PaginatedList<T> : List<T>
    {
        public int PageIndex { get; private set; }
        public int TotalPages { get; private set; }

        public PaginatedList(List<T> items, int count, int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            TotalPages = (int)Math.Ceiling(count / (double)pageSize);

            this.AddRange(items);
        }

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

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

        public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int pageIndex, int pageSize)
        {
            var count = await source.CountAsync();
            var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
            return new PaginatedList<T>(items, count, pageIndex, pageSize);
        }

        internal static Task<object> CreateAsync(object p, int v, int pageSize)
        {
            throw new NotImplementedException();
        }
    }
}
question from:https://stackoverflow.com/questions/65890451/asp-net-core-pagination-not-working-on-datatable-stored-procedure

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
internal static Task<object> CreateAsync(object p, int v, int pageSize)

Is being called instead of

public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int pageIndex, int pageSize)

because every parameter matches "object p" including parameters of type IQueryable

You have to make parameter p more specific or actually implement that method.

More Detail:

Here's what's causing your problem:

 throw new NotImplementedException();

If you implement (write the actual functional code) your error will go away.

But why is it getting top that point in the code (Line 47)

The reason is that List listOfStudents is also an object type because everything in C# extends object. So that means that

CreateAsync(listOfStudents, pageNumber ?? 1, pageSize)

matches the signature of

 internal static Task<object> CreateAsync(object p, int v, int pageSize)
    {
        throw new NotImplementedException();
    }

and it gets called and throws the NotImplementedException.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...