Execute the below query to create a table with the above design.
- CREATE TABLE [dbo].[Employee](
- [ID] [bigint] IDENTITY(1,1) NOT NULL,
- [Name] [nvarchar](max) NULL,
- [Designation] [nvarchar](200) NULL,
- [Location] [nvarchar](200) NULL,
- CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
- (
- [ID] ASC))
And, insert a few dummy records to view in our project UI.
Execute the below query to insert the same dummy values.
- SET IDENTITY_INSERT [dbo].[Employee] ON
- GO
- INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Location]) VALUES (1, N'Gnanavel Sekar', N'Software Engineer', N'Chennai')
- GO
- INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Location]) VALUES (3, N'Robert', N'Application Developer', N'Chennai')
- GO
- INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Location]) VALUES (4, N'Ramar', N'TechLead', N'Chennai')
- GO
- SET IDENTITY_INSERT [dbo].[Employee] OFF
- GO
Create Empty Project in ASP.NET MVC
If you are not aware of the process to create ASP.NET MVC empty project, follow the steps 1 to 3
here. Here, I named it as KendoListView.
Integrate EntityFramework
I already discussed in a previous article about how to integrate Entity Framework with MVC. For that, refer
here and once you integrate the Entity Framework with your MVC application, you will get the screenshot, as shown below.
And also, it generates a connection string in web.config file.
- <connectionStrings>
- <add name="CSharpCornerEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=CSharpCorner;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
- </connectionStrings>
Create a Controller and View
To create a Controller, follow the below steps one by one.
Step 1
Right click on Controllers folder under your project and select Add-> Controller.
Step 2
Select Empty controller and click Add.
Step 3
Name your Controller. Here, I named it as "ListViewController" and you can see that on below screen.
Create a View To create a View, right click on the generated action method Index and select "Add View".
You will get to the below screen. Name it as "Index" and select Empty Template without a Model. Click Add.
Enable Kendo UI ListView
Step 1 Call Kendo Scripts and CSS To access the predefined functionality from Kendo UI, we need to call the following scripts and CSS in our application. So, just add the below references in the <head> tag.
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.common.min.css" />
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.silver.min.css" />
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.mobile.all.min.css" />
- <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
- <script src="http://kendo.cdn.telerik.com/2017.1.118/js/kendo.all.min.js"></script>
Step 2 Create a ListView
Add the below codes to your View to create a ListView.
- <body>
- <div id="example">
- <div class="demo-section k-content wide">
- <div id="listView"></div>
- <div id="pager" class="k-pager-wrap"></div>
- </div>
- <script type="text/x-kendo-template" id="template">
- <div class="employee">
- <h2>#:Name#</h2> <br />
- <h3>#:Designation#</h3>
- <h3>#:Location#</h3>
- </div>
- </script>
- <style>
- #listView {
- padding: 10px 5px;
- margin-bottom: -1px;
- min-height: 510px;
- font: inherit;
- }
-
- .employee {
- float: left;
- position: relative;
- width: 300px;
- height: 170px;
- margin: 0 5px;
- border: double;
- background-color: #73C6B6;
- border-radius: 5px;
- color: white;
- }
-
- .employee img {
- width: 110px;
- height: 110px;
- }
-
- .employee h3 {
- margin: 0;
- padding: 3px 5px 0 0;
- overflow: hidden;
- line-height: 1.1em;
- font-size: .9em;
- font-weight: normal;
- text-transform: uppercase;
- color: white;
- }
-
- .employee p {
- visibility: hidden;
- }
-
- .employee:hover p {
- visibility: visible;
- position: absolute;
- width: 110px;
- height: 110px;
- top: 0;
- margin: 0;
- padding: 0;
- line-height: 110px;
- vertical-align: middle;
- text-align: center;
- color: #fff;
- background-color: rgba(0, 0, 0, 0.75);
- transition: background .2s linear, color .2s linear;
- -moz-transition: background .2s linear, color .2s linear;
- -webkit-transition: background .2s linear, color .2s linear;
- -o-transition: background .2s linear, color .2s linear;
- }
-
- .k-listview:after {
- content: ".";
- display: block;
- height: 0;
- clear: both;
- visibility: hidden;
- }
- </style>
- </div>
- </body>
Detailed Description
- <div id="listView"></div>
The above div will act as listview and all the data is appended here.
- <script type="text/x-kendo-template" id="template">
- <div class="employee">
- <h2>#:Name#</h2> <br />
- <h3>#:Designation#</h3>
- <h3>#:Location#</h3>
- </div>
- </script>
- <style>
The above Kendo template will help us to access the data from Kendo ListView and all database data is loaded here. Here, we mentioned to load only the name, designation, and location. Our database column should be present between the # symbol to load the database data to the kendo listview template.
Enable Kendo listview controller to HTML 5 div "Listview"
To enable kendo listview to our HTML 5 div, add the kendoListView() to the respective Controller. In below code, you can see that "ListView" represents div name.
- $("#listView").kendoListView();
Write logic in controller to load data from database
Write the below codes in your Controller to load the data from database using Entity Framework.
- [HttpGet]
- public JsonResult GetData()
- {
- CSharpCornerEntities ce = new CSharpCornerEntities();
- var result = ce.Employees.ToList();
- return Json(result, JsonRequestBehavior.AllowGet);
- }
Call MVC Action Method "GetData" in jQuery AJAX to bind the data to Kendo ListView.
- <script type="text/javascript">
- $(document).ready(function() {
- $(function() {
- $.ajax({
- type: "GET",
- url: "/ListView/GetData",
- contentType: "application/json; charset=utf-8",
- dataType: "json",
- success: function(responseData) {
- var dataSource = new kendo.data.DataSource({
- data: responseData,
- pageSize: 3
- });
- $("#pager").kendoPager({
- dataSource: dataSource
- });
- $("#listView").kendoListView({
- dataSource: dataSource,
- template: kendo.template($("#template").html())
- });
- },
- failure: function(response) {
- alert(response.responseText);
- },
- error: function(response) {
- alert(response.responseText);
- }
- });
- });
- });
- </script>
Detailed Description
- $.ajax({
- type: "GET",
- - > We need to set whether we getting / posting the data
- url: "/ListView/GetData",
- - > It 's represent url of api / Calling mvc action method, Here ListView represents MVC controller and GetData represents Action method
- contentType: "application/json; charset=utf-8",
- dataType: "json",
- - > Type of data
- success: function(responseData) {
- - > Once this call succeed it will
- return the json data from respective url
- var dataSource = new kendo.data.DataSource({
- data: responseData,
- - > Returned data 's assigned to the kendo datasource
- pageSize: 3 - > Represents list of item needs to be listed in a page
- });
- $("#pager").kendoPager({- > Enabling the pagination
- for listview
- dataSource: dataSource - > The pagination will will work based on this kendo listview databsource
- });
- $("#listView").kendoListView({
- dataSource: dataSource,
- - > Loaded data 's are assigned to kendo listview
- template: kendo.template($("#template").html()) - > Enabling the Kendo template to read or post data from kendo listview
- });
- },
- failure: function(response) {
- alert(response.responseText); - > It will be fired
- if ajax call fails
- },
- error: function(response) {
- alert(response.responseText); - > It will be fired
- if ajax call get 's error
- }
- });
- });
- });
Complete View
- @ {
- ViewBag.Title = "Index";
- } < link rel = "stylesheet"
- href = "http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.common.min.css" / > < link rel = "stylesheet"
- href = "http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.silver.min.css" / > < link rel = "stylesheet"
- href = "http://kendo.cdn.telerik.com/2017.1.118/styles/kendo.mobile.all.min.css" / > < script src = "http://code.jquery.com/jquery-1.12.4.min.js" > < /script> < script src = "http://kendo.cdn.telerik.com/2017.1.118/js/kendo.all.min.js" > < /script> < script type = "text/javascript" > $(document).ready(function() {
- $(function() {
- $.ajax({
- type: "GET",
- url: "/ListView/GetData",
- contentType: "application/json; charset=utf-8",
- dataType: "json",
- success: function(responseData) {
- var dataSource = new kendo.data.DataSource({
- data: responseData,
- pageSize: 3
- });
- $("#pager").kendoPager({
- dataSource: dataSource
- });
- $("#listView").kendoListView({
- dataSource: dataSource,
- template: kendo.template($("#template").html())
- });
- },
- failure: function(response) {
- alert(response.responseText);
- },
- error: function(response) {
- alert(response.responseText);
- }
- });
- });
- }); < /script> < body > < div id = "example" > < div class = "demo-section k-content wide" > < div id = "listView" > < /div> < div id = "pager"
- class = "k-pager-wrap" > < /div> < /div> < script type = "text/x-kendo-template"
- id = "template" > < div class = "employee" > < h2 > #: Name# < /h2> < br / > < h3 > #: Designation# < /h3> < h3 > #: Location# < /h3> < /div> < /script> < style > #listView {
- padding: 10 px 5 px;
- margin - bottom: -1 px;
- min - height: 510 px;
- font: inherit;
- }.employee {
- float: left;
- position: relative;
- width: 300 px;
- height: 170 px;
- margin: 0 5 px;
- padding: 0;
- background - color: #73C6B6;
- border-radius: 5px;
- color: white;
- }
- .employee img {
- width: 110px;
- height: 110px;
- }
- .employee h3 {
- margin: 0;
- padding: 3px 5px 0 0;
- overflow: hidden;
- line-height: 1.1em;
- font-size: .9em;
- font-weight: normal;
- text-transform: uppercase;
- color: white;
- }
- .employee p {
- visibility: hidden;
- }
- .employee:hover p {
- visibility: visible;
- position: absolute;
- width: 110px;
- height: 110px;
- top: 0;
- margin: 0;
- padding: 0;
- line-height: 110px;
- vertical-align: middle;
- text-align: center;
- color: # fff;
- background - color: rgba(0, 0, 0, 0.75);
- transition: background .2 s linear,
- color .2 s linear; - moz - transition: background .2 s linear,
- color .2 s linear; - webkit - transition: background .2 s linear,
- color .2 s linear; - o - transition: background .2 s linear,
- color .2 s linear;
- }.k - listview: after {
- content: ".";
- display: block;
- height: 0;
- clear: both;
- visibility: hidden;
- } < /style> < /div> < /body>
Complete Controller
- using KendoListview.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace KendoListview.Controllers {
- public class ListViewController: Controller {
-
-
- public ActionResult Index() {
- return View();
- }
- [HttpGet]
- public JsonResult GetData() {
- CSharpCornerEntities ce = new CSharpCornerEntities();
- var result = ce.Employees.ToList();
- return Json(result, JsonRequestBehavior.AllowGet);
- }
- }
- }
I Added few more dummy records in database table to perform pagination
Configure RouteConfig.cs
Here, we need to mention the Homepage, that means, we need to mention which Controller and which Action needs to be executed first. By default, it will be in Home and Index. Here, we created the Controller in the name of ListView. So, in the Controller, I mentioned the name "ListView".
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Routing;
- namespace KendoListview {
- public class RouteConfig {
- public static void RegisterRoutes(RouteCollection routes) {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {
- controller = "ListView", action = "Index", id = UrlParameter.Optional
- });
- }
- }
- }
Now, run your application.
Now, change the Pagination length for each page in Kendo ListView. Here, I changed the pageSize from 3 to 5.
- var dataSource = new kendo.data.DataSource({
- data: responseData,
- pageSize: 5
- });
Now, run your application.
Here, we have created an empty project so it can not get build in CSS. So, I have attached the respective CSS under Content folder and referred to a project. Refer the attached project which contains View, Controller, Scripts, and CSS without Entity files.
Summary
In this article, you learned to bind the data to Kendo ListView and how to use it. I hope you got the clear picture about Kendo ListView. For more configuration, visit
here.