In this article, I am going to explain you how to implement AngularJS UI bootstrap pagination in ASP.NET MVC.
Creating ASP.NET MVC Empty Application
Fisrt step is to create ASP.NET MVC empty application as shown below.
Go to File → New → Project. A new window will be open as shown below.
Now go to Web and select .NET Framework 4.5 and give project name and click on OK .
Now new window will open as shown below.
Now Select Empty Template, check on MVC checkbox and click on OK.
Now, a new project will be created as shown below.
Adding Models
Go to Models folder and add new class file Country.cs. Now replace all code with below code.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
namespace
WebApplication1.Models
{
publicclassCountry
{
publicint Id { get; set; }
publicstring Name { get; set; }
publicstring CityName { get; set; }
publicint TotalRecords { get; set; }
}
}
Adding Data Access Layer
Create a Repository.cs class file in root directory of application. Add below code. Here, add one method GetAllCountry which will return list of country. Methods takes 2 parameter PageNo and PageSize to sort and fetch records from database.
using System;
using
System.Collections.Generic;
using
System.Configuration;
using System.Data;
using
System.Data.SqlClient;
using System.Linq;
using System.Web;
using WebApplication1.Models;
namespace WebApplication1
{
publicclassRepository
{
publicIEnumerable<Country> GetAllCountry(int PageNo, int PageSize)
{
List<Country> countryList = null;
try
{
using (SqlConnection con = newSqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ToString()))
select Id,Name,CityName ,row_number()over (orderby id desc)as rowno from
tblCountry
) temp WHERE rowno between((@PageNo-1)*@PageSize)+1 and @PageSize*@PageNo
end
Adding Controller
Next step is to add controller to application. Go to controller folder and add new empty controller named as Home controller. Replace all code with below code. First method is Index which will return index view and second method is GetCountryList which return json data which will be called from index view using ajax method with angularjs.
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using
WebApplication1.Models;
namespace
WebApplication1.Controllers
{
publicclassHomeController : Controller
{
publicActionResult Index()
{
return View();
}
publicActionResult
GetCountryList(
int PageNo, int PageSize)
Now, right click on home controller index action method, add new view and name Index. Select Empty template and uncheck Use layout page and click on Add as shown below.
Now go to Views / Home folder, open Index.cshtml file and replace all code with below code.
Here, 1) angular.js can be downloaded from angular website.
2) ui-bootstrap-tpls-0.13.4.min.js can be downloaded from link provided below. ui-bootstrap-tpls-0.13.4.min.js
In 3) myAngularScript.js file, we will write angular code. In 4) myStyle.css file, we will write pagination and table css.