Paging in ASP.NET MVC part 2

by

Hi, again this is Part 2 from Paging in ASP.NET MVC.

In Part 1, I showed you how to add all data required.

Now let's try to finish what we start together.

Now will try to add new Controller and Call it StudentController.

The most important one is, what I highlight it (Yellow).

First one is declared our Interface,

  private readonly IStudent repositoryStudent;

building contractor.

 public StudentController(IStudent repoStudent)
        {
            repositoryStudent = repoStudent;
        }

 

This one our main job :)

ActionResult Index,

First int? page=1, this for your request, when it will be null, the default page will be first one page=1.

next step is reading data from our Repository.

  var model = repositoryStudent.GetStudent;

if no page was specified in the query string, default to the first page (1)

  var pageNumber = page ?? 1; 

 will only contain 25 products max because of the pageSize

  var onePageOfProducts = products.ToPagedList(pageNumber, 25); 
  

Don't forget to call X.PagedList .

using X.PagedList;

Now to our HTML code.

Just generate Index view.

next Photo will show you what you will have new change.

Again same what we call in our controller, we should call 2 library from X.PagedList.

@using X.PagedList.Mvc;  
@using X.PagedList;

after that to have a good style you need to add,

<link href="/Content/PagedList.css" rel="stylesheet" type="text/css" />

so after all that the most important part is.

@Html.PagedListPager((IPagedList)Model, page => Url.Action("Index", new { page }))

 

The result will be same next photo.

 

 

 

Youtube Video.....

 

Hope this article is helpful.

If you have any question, just write in the comment.

 

Paging in ASP.NET MVC part 1

by

Hi,

Today I'm going to write about Paging in ASP.NET MVC.

First, if someone asks you about paging, why?

Sometimes, we have too many rows, Photos, articles and Products you don't want to display all of them in same time,

or let's say, you don't want load 100K row in one click, this will be a big issue with your application.

  So, the solution will be is paging.

and don't forget to use IQueryable rather than List or IEnumerable.

Now, To work our first step is to create new ASP.NET MVC Project.

Follow the Photos.

 

Choose MVC,

 

Next Step, Add X.PagedList  From NuGet packages.

So, Now we need data to send it to our HTML page and display it in the table.

First, we need to create new Class in Models folder and call it Student.

 

Next Step, Create Folder Call it Services will put inside it,

a new class (IStudent class).

 

 

After Create our Interface, we need to create Repository Folder with StudentRepository class.

In StudentRepository Class, I have fake data just to show you how it will be in our HTML page.

sure you can Use data from your SQL Server database, using Entry Framework.

 

Part2

Onion Architecture in ASP.Net MVC ????? ??????

by

Database initialize class

???? ?? ???? ?????? ????? ???????? ?? ?????? ?????? .

??? ?? ????? ?????? :

 

Repository class implementation

 

?????? ??????? ????? ???? ?????? ??????? ?? ???? ?????????? repository class , ????? ????? ??????? ??? ????? ???????? ??????? LINQ ??? Entity  .

?????? BloodDonorRepository  , ???? ?? ???? ??? ???? ?????? ??  IBloodDonorRepository interface

 

?????? ??????? ?? ?????????? , ??? ?????? LINQ to Entity  ?? ??? ???????? ?? ????? ???????? , ??? ???? ?????? ?????

a blood donor

 

  1. Create object of context class
  2. Use context.entiy.Add(entity)
  3. Use context.SaveChnages()

 

 

???? ?????? ????? ??? ????? ???? ?????????? ????????? ?? ??????? , ????? ??? ??? ????? ??? Build  ??????? , ??????? ?? ????? ????? .

 

Web Project 


??? ???? ?????? ????? MVC  , ???? ????? ??????? ?? ???????? ??????? Core and Interface  , ?? ?????? ??????? ??????? ??? ?????? .

  1. infrastructure project
  2. core project
  3. Entity Framework package

 

??? ????? ?? ??????? , ?? ???? Build ????? ?? ?? ?? ?? ???? .

?? ??????  New Controller  ?? Entity Framework  .??? ?????? ??????  BloodDonorsController  .

????? ????? ?????? ???????? ??????? :

  1. BloodDonor class from the core project as the Model class
  2. BloodDonorContext class as the data context class from the Infrastructure project