Before going through this article ensure that you have a basic understanding of the MVC Architecture, ASP.NET Web API and jQuery.
Let us start with creating an ASP.NET WEB API Application.
Creating an Empty ASP.NET WEB API Project
Create a simple WEB API project as in the following figures:
Creating a Model Class
Right click on the model folder and add a new class, in my case I named it EmployeeList.cs:
Code in EmployeeList.cs:
- class EmployeeList
- {
-
- public EmployeeList(int EmpId, string EmployeeName, string Designation)
- {
- this.EmployeeID = EmpId;
- this.EmployeeName = EmployeeName;
- this.Designation = Designation;
- }
- public int EmployeeID { get; set; }
- public string EmployeeName { get; set; }
- public string Designation { get; set; }
-
-
- }
Creating a Controller
Right click on the Controller folder and add a new WEB API 2- Empty controller as in the figure 3, in my case I named it EmployeeController.cs:
Code in EmployeeController.cs
- [RoutePrefix("api/Employee")]
- public class EmployeeController : ApiController
- {
-
-
- [HttpGet]
- [AllowAnonymous]
- [Route("GetEmployeeListA")]
- public HttpResponseMessage GetEmployeeA()
- {
- try {
- List<EmployeeList> _emp = new List<EmployeeList>();
- _emp.Add(new EmployeeList(1, "Arun", "Software Engineer"));
- _emp.Add(new EmployeeList(2, "Pradeep", "Infrastructure Engineer"));
- _emp.Add(new EmployeeList(3, "Jai", "HR"));
- return Request.CreateResponse(HttpStatusCode.OK, _emp, Configuration.Formatters.JsonFormatter);
- }
- catch(Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.OK, ex.Message, Configuration.Formatters.JsonFormatter);
- }
-
- }
-
-
- [HttpGet]
- [AllowAnonymous]
- [Route("GetEmployeeListB")]
- public HttpResponseMessage GetEmployeeB()
- {
- try
- {
- List<EmployeeList> _emp = new List<EmployeeList>();
- _emp.Add(new EmployeeList(1, "Asif", "Software Engineer"));
- _emp.Add(new EmployeeList(2, "Dinesh", "Infrastructure Engineer"));
- _emp.Add(new EmployeeList(3, "James", "HR"));
- return Request.CreateResponse(HttpStatusCode.OK, _emp, Configuration.Formatters.JsonFormatter);
- }
- catch (Exception ex)
- {
- return Request.CreateResponse(HttpStatusCode.OK, ex.Message, Configuration.Formatters.JsonFormatter);
- }
-
- }
-
-
- }
The above employee controller action GetEmployeeA and GetEmployeeB will return an employee list of company A and company B respectively.
API Test
Test the API using the POSTMAN/Fiddler as in the following figure 4 & 5:
API End Point: /api/Employee/GetEmployeeListA
Type : GET
API End Point: /api/Employee/GetEmployeeListB
Type : GET
The API is working fine, now it's ready to consume.
Using a Kendo Grid with MVVM pattern
Please read my previous article to get more details about implementing Kendo Grid.
Creating a HTML page
Create a new HTML page in the project.
Design:
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- <title></title>
- <meta charset="utf-8" />
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.226/styles/kendo.common.min.css">
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.226/styles/kendo.rtl.min.css">
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.226/styles/kendo.default.min.css">
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.226/styles/kendo.mobile.all.min.css">
-
- <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
- <script src="http://kendo.cdn.telerik.com/2016.1.226/js/angular.min.js"></script>
- <script src="http://kendo.cdn.telerik.com/2016.1.226/js/jszip.min.js"></script>
- <script src="http://kendo.cdn.telerik.com/2016.1.226/js/kendo.all.min.js"></script>
-
- <meta charset="utf-8" />
- </head>
- <body>
- <div id="example">
- <div class="row">
-
- <button id="Btn" class="k-button" data-bind="click:DisplayGridA">Show Company A</button>
- <button id="Btn_B" class="k-button" data-bind="click:DisplayGridB">Show Company B</button>
- </div>
- <br/>
- <br />
- <div class=row>
- <div id="grid" data-role="grid" data-filterable="true" data-pegeable="true"
- data-columns="[
- {'field':'EmployeeName','title':'Name'},
- {'field':'Designation','title':'Designation'},
-
-
- ]"
- data-bind="source: dataSourceA,visible: isVisible," style="height: 200px"
- </div>
- </div>
- </div>
- <script>
- var viewModel = kendo.observable({
- isVisible: true,
- DisplayGridA:function(e)
- {
- e.preventDefault();
- var grid = $("#grid").data("kendoGrid");
- grid.setDataSource(viewModel.dataSourceA)
-
- },
- DisplayGridB: function (e) {
- e.preventDefault();
- var grid = $("#grid").data("kendoGrid");
- grid.setDataSource(viewModel.dataSourceB)
-
- },
- dataSourceA: new kendo.data.DataSource({
- schema: {
- model: {
- id: "EmpId",
- fields: {
- EmployeeName: { type: "string" },
- Designation: { type: "string" }
- }
- }
- },
- batch: true,
- transport: {
- read: {
- url: "/api/Employee/GetEmployeeListA",
- dataType: "json"
- },
-
- parameterMap: function(options, operation) {
- if (operation !== "read" && options.models) {
- return {models: kendo.stringify(options.models)};
- }
- }
- }
- }),
- dataSourceB: new kendo.data.DataSource({
- schema: {
- model: {
- id: "EmpdId",
- fields: {
- EmployeeName: { type: "string" },
- Designation: { type: "string" }
- }
- }
- },
- batch: true,
- transport: {
- read: {
- url: "/api/Employee/GetEmployeeListB",
- dataType: "json"
- },
-
- parameterMap: function (options, operation) {
- if (operation !== "read" && options.models) {
- return { models: kendo.stringify(options.models) };
- }
- }
- }
- })
- });
- kendo.bind($("#example"), viewModel);
- </script>
- </body>
- </html>
Initially the kendo grid is bind with dataSourceA which is set as source of kendo grid
In our design we have two buttons which are responsible to switch the kendo grid dataSource.
- Show Company A – Button:
This button is used to trigger the DisplayGridA function which is used to set the dataSourceA as DataSource of the kendo grid using the setDataSource function.
setDataSource: This function is used to set the datasource of kendo widget(in our case it is grid).
2. Show Company B – Button
This button is used to trigger the DisplayGridB function which is used to set the dataSourceB as DataSource of the kendo grid using the setDataSource function
Result in Browser, while clicking the Show Company A Button
Result in Browser, while clicking the Show Company B Button
I hope you have enjoyed this article. Your valuable feedback, question, or comments about this article are always welcomed.
Read more articles on jQuery: