View Product Details Using Kendo ScrollView In ASP.NET MVC5 With Entity Framework

Here, I will demonstrate how to work with Kendo ScrollView to View the product details from a database using Entity Framework in ASP.Net MVC5

Introduction
 
 In this article, I will demonstrate how to work with Kendo ScrollView to View the product details from a database using Entity Framework in ASP.Net MVC5. Let's move forward
 
Prerequisites 
  • Visual Studio
  • SQL Server
  • Basic Knowledge of ASP.Net MVC
  • Basic Knowledge of Entity Framework
  • Basic Knowledge of JQuery
  • Basic Knowledge of CSS
Article Flow 
  • Create a table in database with List of product values
  • Create ASP.NET MVC Empty project
  • Configure Entity Framework with database and application
  • Create a Controller and View
  • Enable Kendo UI Features
  • Load Products to ScrollView 
Create a table in database with List of product values
 
First, we will create a table in SQL Server to populate a Kendo Scrollview with the product(s) details in ASP.NET MVC Web application. I have created a table called "Product" with the following design.
 
 
 
Execute the below query to create a table with the above design.
  1. CREATE TABLE [dbo].[Product] (  
  2.   [ProductID] [int] IDENTITY (1, 1) NOT NULL,  
  3.   [Name] [nvarchar](300) NULL,  
  4.   [Type] [nvarchar](100) NULL,  
  5.   [Price] [floatNULL,  
  6.   [Description] [nvarchar](MAXNULL,  
  7.   [ImageURL] [nvarchar](MAXNULL,  
  8.   [CreatedOn] [datetime] NULL,  
  9.   CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED  
  10.   (  
  11.   [ProductID] ASC  
  12.   ) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  13. ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]  
  14. GO  
And now, add a few dummy values to view in Kendo UI ScrollView. I have added some rows as shown below.
 
 
To add these dummy products, execute the below insert queries.
  1. SET IDENTITY_INSERT [dbo].[Product] ON  
  2. GO  
  3. INSERT [dbo].[Product] ([ProductID], [Name], [Type], [Price], [Description], [ImageURL], [CreatedOn])  
  4.   VALUES (1, N'Pen', N'Education', 25, N'A pen is a writing instrument', N'Pen'CAST(0x0000A8390001E88B AS datetime))  
  5. GO  
  6. INSERT [dbo].[Product] ([ProductID], [Name], [Type], [Price], [Description], [ImageURL], [CreatedOn])  
  7.   VALUES (2, N'Pencil', N'Education', 10, N'A pencil is a writing implement', N'Pencil'CAST(0x0000A8390001E88B AS datetime))  
  8. GO  
  9. INSERT [dbo].[Product] ([ProductID], [Name], [Type], [Price], [Description], [ImageURL], [CreatedOn])  
  10.   VALUES (3, N'Tomato', N'Vegitable', 25, N'The tomato is the edible, often red, fruit of the plant Solanum lycopersicum', N'Tomato'CAST(0x0000A8390001E88B AS datetime))  
  11. GO  
  12. INSERT [dbo].[Product] ([ProductID], [Name], [Type], [Price], [Description], [ImageURL], [CreatedOn])  
  13.   VALUES (4, N'Potato', N'Vegitable', 40, N'NULLThe potato is a starchy, tuberous crop from the perennial nightshade Solanum tuberosum. The word "potato" may refer either to the plant itself or to the edible tuber', N'Potato'CAST(0x0000A8390001E88B AS datetime))  
  14. GO  
  15. SET IDENTITY_INSERT [dbo].[Product] OFF  
  16. GO  
  17. ALTER TABLE [dbo].[Product] ADD CONSTRAINT [DF_Product] DEFAULT (GETDATE()) FOR [CreatedOn]  
  18. GO  
Create ASP.NET MVC Empty project
 
To create ASP.NET MVC empty project, follow the below steps one by one. Here, I have used Visual Studio 2013.
  1. Select New Project -> Visual C# -> Web -> ASP.NET Web Application and enter your application name. Here, I named it "KendoScrollViewInMVC5".
  2. Now, click OK.
  3. Then, select Empty ASP.Net MVC template and click OK to create the project.
  4. Once you click OK, the project will be created with the basic architecture of MVC.If you are not aware of how to create an Empty ASP.NET Web Application, please visit Step 1 and Step 2 to learn. 
 Once you complete these steps you will get the screen as below
 
 
 
 Configure Entity Framework with database and application
 
Here, I already discussed how to configure and implement database first approach. In the meantime choose our created table with entity framework, once we do our configuration with SQL table "Product" from CSharpCorner database and with our application you will get the below screen as succeeding configuration. 
 
 
 
Create a Controller and View
 
Now create an empty controller and view, here I created a controller with the name of "ScrollViewController".Whenever we create an empty Controller, it is created with empty Index action method. And create an empty view to this action method "Index".
 
Enable Kendo UI Features
 
Here, we going to enable the Kendo UI features with our application by adding the below CSS and JS in our shared _Layout view.
  1. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1026/styles/kendo.common-material.min.css" />  
  2. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1026/styles/kendo.material.min.css" />  
  3. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1026/styles/kendo.material.mobile.min.css" />  
  4. <script src="https://kendo.cdn.telerik.com/2017.3.1026/js/jquery.min.js"></script>  
  5. <script src="https://kendo.cdn.telerik.com/2017.3.1026/js/kendo.all.min.js"></script>  
Now create a divs in view which will act as scroll view with previous and next image buttons
  1. <div id="example" style="width:570px;">  
  2.     <div class="demo-section hidden-on-narrow k-content">  
  3.         <div id="scrollview" style="height:570px !important;"></div> <a id="prev-img" title="Previous Image"></a> <a id="next-img" title="Next Image"></a>   
  4. </div>  
And add the below kendo template to load the database data to scroll view slider. In below code, you can see that these(#:ImageURL#,#:Name #,#:Type #,#:Price #,#:Description #) fields, these fields are the database column name and we can declare it between # with colon to display the data in view.
  1. <script id="template" type="text/x-kendo-template">  
  2.     <div>  
  3.         <div class="image" style="background-image: url('../Content/Products/#:ImageURL#.jpg');"></div>  
  4.         <p class="title">#:Name #</p>  
  5.     </div>  
  6.     <div>  
  7.         <p>Type : #:Type #</p>  
  8.         <p>Price : Rs. #:Price # </p>  
  9.         <p>Details: #:Description # </p>  
  10.     </div>  
  11. </script>  
In above code, you can see that the name of the product has been saved in database and file has stored in physical as below and loaded from that respective path
 
 
And, add the script given below to enable the kendoScrollview plugin to the respective control. In the below code, you can see that the products are loading with the help of transport read and there we mentioned the path of action. 
  1. <script type="text/javascript">  
  2.     $(function() {  
  3.         $("#scrollview").kendoMobileScrollView({  
  4.             dataSource: {  
  5.                 type: "json",  
  6.                 transport: {  
  7.                     read: "http://localhost:5157/ScrollView/GetProducts"  
  8.                 }  
  9.             },  
  10.             serverPaging: true,  
  11.             pageSize: 30,  
  12.             schema: {  
  13.                 data: "photos",  
  14.                 total: "total_items"  
  15.             },  
  16.             contentHeight: "440px",  
  17.             enablePager: false,  
  18.             template: kendo.template($("#template").html())  
  19.         });  
  20.         $("#prev-img").click(function(e) {  
  21.             var scrollView = $("#scrollview").data("kendoMobileScrollView");  
  22.             scrollView.prev();  
  23.         });  
  24.         $("#next-img").click(function(e) {  
  25.             var scrollView = $("#scrollview").data("kendoMobileScrollView");  
  26.             scrollView.next();  
  27.         });  
  28.     });  
  29. </script>  
Write the below logic in a controller to load the data from a database and return it as JSON type.
  1. public ActionResult GetProducts()  
  2. {  
  3.     CSharpCornerEntities entities = new CSharpCornerEntities();  
  4.     var products = entities.Products.ToList();  
  5.     return Json(products, JsonRequestBehavior.AllowGet);  
  6. }  
Now add the below CSS to make the scroll viewer control look good
  1. <style>  
  2.     .demo-section {  
  3.         position: relative;  
  4.     }  
  5.   
  6.     .demo-section>p {  
  7.         text-align: right;  
  8.         margin-top: 10px;  
  9.         font-size: .8em;  
  10.     }  
  11.  
  12.     #prev-img,  
  13.     #next-img {  
  14.         display: block;  
  15.         position: absolute;  
  16.         top: 42px;  
  17.         z-idex: 1;  
  18.         height: 400px;  
  19.         width: 100px;  
  20.         opacity: 0.2;  
  21.     }  
  22.  
  23.     #prev-img {  
  24.         left: 42px;  
  25.         background: url('../Content/Arrow/arrow-left.ico') no-repeat 50% 50%;  
  26.     }  
  27.  
  28.     #next-img {  
  29.         left: auto;  
  30.         right: 42px;  
  31.         background: url('../Content/Arrow/arrow-right.ico') no-repeat 50% 50%;  
  32.     }  
  33.   
  34.     a#prev-img:hover {  
  35.         background: url('../Content/Arrow/arrow-left.ico') no-repeat 50% 50% rgba(0, 0, 0, .3);  
  36.         opacity: 1;  
  37.     }  
  38.   
  39.     a#next-img:hover {  
  40.         background: url('../Content/Arrow/arrow-right.ico') no-repeat 50% 50% rgba(0, 0, 0, .3);  
  41.         opacity: 1;  
  42.     }  
  43.   
  44.     .title {  
  45.         font-weight: bold;  
  46.         text-transform: uppercase;  
  47.         text-align: center;  
  48.         margin: 0;  
  49.         padding: 1em;  
  50.         background-color: #222;  
  51.         color: #fff;  
  52.     }  
  53.   
  54.     .image {  
  55.         display: block;  
  56.         height: 450px;  
  57.         width: 550px;  
  58.         background-size: cover;  
  59.     }  
  60. </style>  
Okay now change the controller name in RouteConfig.cs file as below 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7. namespace KendoScrollViewInMVC5 {  
  8.     public class RouteConfig {  
  9.         public static void RegisterRoutes(RouteCollection routes) {  
  10.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  11.             routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {  
  12.                 controller = "ScrollView", action = "Index", id = UrlParameter.Optional  
  13.             });  
  14.         }  
  15.     }  
  16. }  
Run your application
 
 
 
Complete View 
 
_Layout.cshtml 
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">  
  7.     <title>@ViewBag.Title - Kendo ScrollView</title>  
  8.     <link href="~/Content/Site.css" rel="stylesheet" type="text/css" />  
  9.     <link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />  
  10.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1026/styles/kendo.common-material.min.css" />  
  11.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1026/styles/kendo.material.min.css" />  
  12.     <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.1026/styles/kendo.material.mobile.min.css" />  
  13.     <script src="https://kendo.cdn.telerik.com/2017.3.1026/js/jquery.min.js"></script>  
  14.     <script src="https://kendo.cdn.telerik.com/2017.3.1026/js/kendo.all.min.js"></script>  
  15. </head>  
  16.   
  17. <body>  
  18.     <div class="navbar navbar-inverse navbar-fixed-top">  
  19.         <div class="container">  
  20.             <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">  
  21. <span class="icon-bar"></span>  
  22. <span class="icon-bar"></span>  
  23. <span class="icon-bar"></span>  
  24. </button> @Html.ActionLink("Kendo ScollView with ASP.NET MVC5""Index""Home"nullnew { @class = "navbar-brand" }) </div>  
  25.             <div class="navbar-collapse collapse">  
  26.                 <ul class="nav navbar-nav"> </ul>  
  27.             </div>  
  28.         </div>  
  29.     </div>  
  30.     <div class="container body-content"> @RenderBody()  
  31.         <hr /> </div>  
  32. </body>  
  33.   
  34. </html>  
Index.cshtml
  1. @ {  
  2.     ViewBag.Title = "Index";  
  3. } < div id = "example"  
  4. style = "width:570px;" > < div class = "demo-section hidden-on-narrow k-content" > < div id = "scrollview"  
  5. style = "height:570px !important;" > < /div> < a id = "prev-img"  
  6. title = "Previous Image" > < /a> < a id = "next-img"  
  7. title = "Next Image" > < /a> < /div> < div class = "responsive-message" > < /div> < /div> < script id = "template"  
  8. type = "text/x-kendo-template" > < div > < div class = "image"  
  9. style = "background-image: url('../Content/Products/#:ImageURL#.jpg');" > < /div> < p class = "title" > #: Name# < /p> < /div> < div > < p > Type: #: Type# < /p> < p > Price: Rs.#: Price# < /p> < p > Details: #: Description# < /p> < /div> < /script> < script type = "text/javascript" > $(function() {  
  10.     $("#scrollview").kendoMobileScrollView({  
  11.         dataSource: {  
  12.             type: "json",  
  13.             transport: {  
  14.                 read: "http://localhost:5157/ScrollView/GetProducts"  
  15.             }  
  16.         },  
  17.         serverPaging: true,  
  18.         pageSize: 30,  
  19.         schema: {  
  20.             data: "photos",  
  21.             total: "total_items"  
  22.         },  
  23.         contentHeight: "440px",  
  24.         enablePager: false,  
  25.         template: kendo.template($("#template").html())  
  26.     });  
  27.     $("#prev-img").click(function(e) {  
  28.         var scrollView = $("#scrollview").data("kendoMobileScrollView");  
  29.         scrollView.prev();  
  30.     });  
  31.     $("#next-img").click(function(e) {  
  32.         var scrollView = $("#scrollview").data("kendoMobileScrollView");  
  33.         scrollView.next();  
  34.     });  
  35. }); < /script> < style > .demo - section {  
  36.     position: relative;  
  37. }.demo - section > p {  
  38.     text - align: right;  
  39.     margin - top: 10 px;  
  40.     font - size: .8e m;  
  41. }#  
  42. prev - img, #next - img {  
  43.     display: block;  
  44.     position: absolute;  
  45.     top: 42 px;  
  46.     z - idex: 1;  
  47.     height: 400 px;  
  48.     width: 100 px;  
  49.     opacity: 0.2;  
  50. }#  
  51. prev - img {  
  52.     left: 42 px;  
  53.     background: url('../Content/Arrow/arrow-left.ico') no - repeat 50 % 50 % ;  
  54. }#  
  55. next - img {  
  56.     left: auto;  
  57.     right: 42 px;  
  58.     background: url('../Content/Arrow/arrow-right.ico') no - repeat 50 % 50 % ;  
  59. }  
  60. a# prev - img: hover {  
  61.     background: url('../Content/Arrow/arrow-left.ico') no - repeat 50 % 50 % rgba(0, 0, 0, .3);  
  62.     opacity: 1;  
  63. }  
  64. a# next - img: hover {  
  65.     background: url('../Content/Arrow/arrow-right.ico') no - repeat 50 % 50 % rgba(0, 0, 0, .3);  
  66.     opacity: 1;  
  67. }.title {  
  68.     font - weight: bold;  
  69.     text - transform: uppercase;  
  70.     text - align: center;  
  71.     margin: 0;  
  72.     padding: 1e m;  
  73.     background - color: #222;  
  74. color: # fff;  
  75. }.image {  
  76.     display: block;  
  77.     height: 450 px;  
  78.     width: 550 px;  
  79.     background - size: cover;  
  80. } < /style>  
Complete Controller 
  1. using KendoScrollViewInMVC5.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7. namespace KendoScrollViewInMVC5.Controllers {  
  8.     public class ScrollViewController: Controller {  
  9.         //  
  10.         // GET: /ScrollView/  
  11.         public ActionResult Index() {  
  12.             return View();  
  13.         }  
  14.         public ActionResult GetProducts() {  
  15.             CSharpCornerEntities entities = new CSharpCornerEntities();  
  16.             var products = entities.Products.ToList();  
  17.             return Json(products, JsonRequestBehavior.AllowGet);  
  18.         }  
  19.     }  
  20. }  
Here, we have created an empty project so it can not get built in CSS. So, I have attached the respective CSS under Content folder and referred it to a project. Refer to the attached project for reference and I did attach the demonstrated project without a package for Entity Framework 6.0 due to the size limit.
 
Summary
 
In this article, we have seen how to view the product details in our ASP.NET MVC5 web application with Kendo ScrollView using Entity Framework.

I hope you enjoyed this article. Your valuable feedback and comments about this article are always welcome.