Before going through this article ensure that you have a basic Knowledge of MVC Architecture and ASP.NET Web API.

Let we start with creating a REST service using WEB API.

Just create a WEB API project in Visual Studio as shown in Figures 1 and 2:


Figure 1


Figure 2

Creating a Model Classes

Right-click on the model folder and create a class, in my case I named it Product.

Write the following code in the Product.cs model class.
  1. public class Product {
  2. [Key]
  3. [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  4. public int ProductID {
  5. get;
  6. set;
  7. }
  8. [Required]
  9. public string ProductName {
  10. get;
  11. set;
  12. }
  13. [Required]
  14. public string UnitPrice {
  15. get;
  16. set;
  17. }
  18. }

Here I am using Entity Framework Code first technique so we need to create the context class.

Right-click on the model folder and create one more class, in my case I named it ProductContext.

Write the following code in ProductContext class.

  1. public class ProductContext: DbContext {
  2. public ProductContext(): base("name=TestConnection") {}
  3. public DbSet < Product > Products {
  4. get;
  5. set;
  6. }
  7. }
Scaffolding the WEB API Controller Class

Note: Before doing Scaffolding build your application once.

Right-click on Controller folder then selelct Add--> Controller and create a WEB API class as shown in 3 and 4.

Figure 3

Figure 4

The preceding procedure will scaffold the REST full service in the ProductsController.cs.

You will get some pre-defined HTTP GET, POST, PUT and DELETE requests/responses in the products Controller. Modify the code based on your application requirements. For this example I didn't modify the code.

Now the REST services are created , it's time to create a Kendo UI List View to consume the services.

Before implementing the service in the Kendo UI once check that in Postman / Fiddler.

Creating a Kendo List View with Remote Binding

Create a HMTL page in your project, in my case I named it KendoList.html.

Desgin in KendoList.html

  1. <html
  2. xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.common.min.css" />
  5. <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.default.min.css" />
  6. <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.dataviz.min.css" />
  7. <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.dataviz.default.min.css" />
  8. <script src="http://cdn.kendostatic.com/2014.3.1316/js/jquery.min.js"></script>
  9. <script src="http://cdn.kendostatic.com/2014.3.1316/js/kendo.all.min.js"></script>
  10. <title></title>
  11. </head>
  12. <body>
  13. <style>
  14. .product-view {
  15. float: left;
  16. width: 50%;
  17. height: 300px;
  18. box-sizing: border-box;
  19. border-top: 0;
  20. position: relative;
  21. }
  22. .product-view:nth-child(even) {
  23. border-left-width: 0;
  24. }
  25. .product-view dl {
  26. margin: 10px 10px 0;
  27. padding: 0;
  28. overflow: hidden;
  29. }
  30. .product-view dt, dd {
  31. margin: 0;
  32. padding: 0;
  33. width: 100%;
  34. line-height: 24px;
  35. font-size: 18px;
  36. }
  37. .product-view dt {
  38. font-size: 11px;
  39. height: 16px;
  40. line-height: 16px;
  41. text-transform: uppercase;
  42. opacity: 0.5;
  43. }
  44. .product-view dd {
  45. height: 46px;
  46. overflow: hidden;
  47. white-space: nowrap;
  48. text-overflow: ellipsis;
  49. }
  50. .product-view dd .k-widget,
  51. .product-view dd .k-textbox {
  52. font-size: 14px;
  53. }
  54. .k-listview {
  55. border-width: 1px 1px 1px 0;
  56. padding: 0;
  57. overflow: hidden;
  58. min-height: 298px;
  59. }
  60. .edit-buttons {
  61. position: absolute;
  62. bottom: 0;
  63. left: 0;
  64. right: 0;
  65. text-align: right;
  66. padding: 5px;
  67. background-color: rgba(0,0,0,0.1);
  68. }
  69. span.k-invalid-msg {
  70. position: absolute;
  71. margin-left: 6px;
  72. }
  73. .k-add-button {
  74. margin-bottom: 2em;
  75. }
  76. @media only screen and (max-width : 620px) {
  77. .product-view {
  78. width: 100%;
  79. }
  80. .product-view:nth-child(even) {
  81. border-left-width: 1px;
  82. }
  83. }
  84. </style>
  85. <div id="example">
  86. <div class="demo-section k-header wide">
  87. <div class="container">
  88. <div class="row">
  89. <div class="col-md-3">
  90. <label>Product Name</label>
  91. </div>
  92. <div class="col-md-3">
  93. <input type="text" id="ProductName" data-bind="value:ProductName" class="k-textbox" />
  94. </div>
  95. </div>
  96. <div class="row">
  97. <div class="col-md-3">
  98. <label>Unit Cost</label>
  99. </div>
  100. <div class="col-md-3">
  101. <input type="text" id="UnitPrice" class="k-textbox" data-bind="value:UnitPrice" />
  102. </div>
  103. </div>
  104. <br/>
  105. <br/>
  106. <div class="row">
  107. <button data-role="button"
  108. data-sprite-css-class="k-icon k-edit"
  109. data-bind="events: { click: InsertProduct }"
  110. style="width: 180px">
  111. Submit
  112. </button>
  113. </div>
  114. </div>
  115. <div>
  116. <h4>Kendo List</h4>
  117. <div data-role="listview"
  118. data-template="template"
  119. data-bind="source: products,
  120. visible: isVisible"
  121. style="height: 300px; overflow: auto"></div>
  122. </div>
  123. </div>
  124. <script type="text/x-kendo-tmpl" id="template">
  125. <div class="product-view k-widget">
  126. <dl>
  127. <dt>Product Name</dt>
  128. <dd>#:ProductName#</dd>
  129. <dt>Unit Price</dt>
  130. <dd>#:kendo.toString(UnitPrice, "c")#</dd>
  131. </dl>
  132. </div>
  133. </script>
  134. </div>
JavaScipt with MVVM Model

  1. $(document).ready(function() {
  2. var viewModel = kendo.observable({
  3. isVisible: true,
  4. ProductName: '',
  5. UnitPrice: '',
  6. products: new kendo.data.DataSource({
  7. batch: true,
  8. transport: {
  9. read: {
  10. url: "api/Products",
  11. dataType: "json"
  12. },
  13. parameterMap: function(options, operation) {
  14. if (operation !== "read" && options.models) {
  15. return {
  16. models: kendo.stringify(options.models)
  17. };
  18. }
  19. }
  20. },
  21. schema: {
  22. model: {
  23. id: "ProductID",
  24. }
  25. },
  26. }),
  27. InsertProduct: function(e) {
  28. e.preventDefault()
  29. $.ajax({
  30. type: "POST",
  31. url: 'api/Products',
  32. data: {
  33. ProductName: $("#ProductName").val(),
  34. UnitPrice: $("#UnitPrice").val()
  35. },
  36. })
  37. }
  38. });
  39. kendo.bind($("#example"), viewModel);
  40. })
Result in a browser


Figure 5

To show the structure of the Kendo list I already inserted one product into it.

We will start to add one more new product as shown in 6 and 7.


Figure 6

Figure 7

Check the table in your SQL database:


Yes, the records are inserted into the table.

That's it, I hope you have enjoyed this article, thank you.

Happy Coding.