0

I got a trouble in my asp.net proyect. I want to paginate my table but i'm unable to launch my site.

That's the problem:

  • I created entity model into 'Context' folder with getters and setters of my table
  • Then i generated the controller with vs
  • I modify controller with this code

        public ActionResult Index(int? page)
        {
    
        var firmas = from f in db.Firmas select f;
        if (Request.HttpMethod != "GET")
        {
            page = 1;
        }
        int pageSize = 2;
        int pageNumber = (page ?? 1);
    
        return View(firmas.ToPagedList(pageSize, pageNumber));
        }
    

And i got this into cshtml index

@*@model IEnumerable<actiTest2.Context.Firmas>*@

@model PagedList.IPagedList<actiTest2.Controllers.FirmasController>

@using PagedList;
@using PagedList.Mvc;

That's the point, if i call model IEnumerable, program crash cause need IPagedList, but if i call IPagedList program say that 'FirmasController' does not contain a definition for my model display.

What could i do? Merge Controller and auto-generated code with get/set errs?

Thanks in advance

EDIT: After apply the 'smoksnes' solution i got a new problem:

https://i.stack.imgur.com/2brzG.png

David C.
  • 25
  • 4
  • Its `@model PagedList.IPagedList` (assuming `db.Firmas` returns objects that are typeof `Firmas`) –  Jul 21 '16 at 11:33

1 Answers1

0

Change your model in your view to (first line):

@model PagedList.IPagedList<actiTest2.Context.Firmas>

When you use @model PagedList.IPagedList<actiTest2.Controllers.FirmasController> your're trying to use your controller as a model.

So your view should look like this:

@using PagedList
@using PagedList.Mvc
@model PagedList.IPagedList<actiTest2.Context.Firmas>

<p> Content here </p>

And in your controller:

public ActionResult Index(int? page)
{
    // Order the collection before skipping.
    var firmas = db.Firmas.OrderBy(x => x.Name); // Or some property.
    if (Request.HttpMethod != "GET")
    {
        page = 1;
    }
    int pageSize = 2;
    int pageNumber = (page ?? 1);

    return View(firmas.ToPagedList(pageSize, pageNumber));
}

More info about error here:

The method 'OrderBy' must be called before the method 'Skip' Exception

The exception means that you always need a sorted input if you apply Skip, also in the case that the user doesn't click on a column to sort by.

And specifically for ToPagedList().

Community
  • 1
  • 1
smoksnes
  • 10,509
  • 4
  • 49
  • 74