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 empty WEB API project as in the following figure:

Figure 1
'
Figure 2

Creating a Model Class

Right click on the model folder and add a new class, in my case I named it CommonList.cs:

Code in CommonList.cs
  1. public class CommonList
  2. {
  3. public CommonList(int Id, string Text)
  4. {
  5. this.Id = Id;
  6. this.Text = Text;
  7. }
  8. public int Id { get; set; }
  9. public string Text { get; set; }
  10. }

Creating a Controller

Right click on the Controller folder and add a new controller, in my case I named it ListsitemsController.cs:

Code in ListsitemsController.cs
  1. [RoutePrefix("api/list")]
  2. public class ListsitemsController : ApiController
  3. {
  4. [HttpGet]
  5. [Route("items")]
  6. public HttpResponseMessage ListItems()
  7. {
  8. List<CommonList> _list = new List<CommonList>();
  9. _list.Add(new CommonList(1, "India"));
  10. _list.Add(new CommonList(2, "China"));
  11. _list.Add(new CommonList(3, "United States"));
  12. List<CommonList> _list1 = new List<CommonList>();
  13. _list1.Add(new CommonList(1, "TamilNadu"));
  14. _list1.Add(new CommonList(2, "Karnataka"));
  15. _list1.Add(new CommonList(3, "NewYork"));
  16. return this.JsonResponse(
  17. new
  18. {
  19. CountryList = _list,
  20. StateList = _list1,
  21. });
  22. }
  23. public HttpResponseMessage JsonResponse(object response, HttpStatusCode httpStatusCode = HttpStatusCode.OK)
  24. {
  25. return Request.CreateResponse(httpStatusCode, response, Configuration.Formatters.JsonFormatter);
  26. }
  27. }
The above controller ListItems action will return a Country list and State list as a response.
Now the API is ready to consume.

Using a Kendo Dropdownlist with MVVM pattern
Please read my previous article to get more details about implementing kendo dropdownlist with MVVM Pattern.

Creating a HTML page

Create a new HTML page in the project.

Design:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Kendo Dropdownlist MVVM</title>
  5. <meta charset="utf-8" />
  6. <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.112/styles/kendo.common.min.css">
  7. <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.112/styles/kendo.rtl.min.css">
  8. <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.112/styles/kendo.default.min.css">
  9. <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.112/styles/kendo.mobile.all.min.css">
  10. <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  11. <script src="http://kendo.cdn.telerik.com/2016.1.112/js/angular.min.js"></script>
  12. <script src="http://kendo.cdn.telerik.com/2016.1.112/js/jszip.min.js"></script>
  13. <script src="http://kendo.cdn.telerik.com/2016.1.112/js/kendo.all.min.js"></script>
  14. <script src="scripts/Core.js"></script>
  15. <script src="scripts/Data.js"></script>
  16. <script src="scripts/ViewModel.js"></script>
  17. </head>
  18. <body>
  19. <div class="col-lg-12">
  20. <div id="Main" class="main"></div>
  21. </div>
  22. <script type="text/x-kendo-template" id="Layout-temp">
  23. <div class="col-lg-12">
  24. <div id="content"></div>
  25. </div>
  26. </script>
  27. <script type="text/x-kendo-template" id="Dashboard-temp">
  28. <div class="row">
  29. <h4>Kendo DropDownlist with MVVM Pattern</h4>
  30. <input data-role="dropdownlist"
  31. data-bind="source:state,value:State"
  32. data-text-field="Text" data-value-field="Id" data-option-label="--Select State--"
  33. style="width:50%" />
  34. <br />
  35. <br />
  36. <br />
  37. <input data-role="dropdownlist"
  38. data-bind="source:country,value:Country"
  39. data-text-field="Text" data-value-field="Id" data-option-label="--Select Country--"
  40. style="width:50%" />
  41. </div>
  42. </script>
  43. </body>
  44. </html>

Creating a JavaScript files

  1. View Model: Create a JavaScript file, in my case I named it ViewModel.Js

    The View-Model is a representation of your data (the Model) which will be displayed in the View.

    ViewModel.Js
    1. (function (G, $, undefined) {
    2. $.extend(true, G, {
    3. KendoDropdown: {
    4. ViewModel: {
    5. DashboardModel: new kendo.observable({
    6. Country: '',
    7. country: [],
    8. state: [],
    9. State: '',
    10. }),
    11. UpdateList: function (resp) {
    12. this.DashboardModel.set('country', resp.CountryList);
    13. this.DashboardModel.set('state', resp.StateList)
    14. },
    15. }
    16. }
    17. });
    18. })(window.Gni = window.Gni || {}, jQuery);
  2. Data: Create a JavaScript file, in my case I named it Data.Js

    This script is responsible to bound the DataSource by requesting the API.

    Data.js
    1. (function (G, $, K, undefined) {
    2. $.extend(true, G, {
    3. KendoDropdown: {
    4. Data: {
    5. List: new K.data.DataSource({
    6. data: {},
    7. transport: {
    8. read: {
    9. url: 'api/list/items',
    10. type: 'get',
    11. dataType: 'json',
    12. }
    13. },
    14. schema: {
    15. data: function (response) {
    16. return [response]; // create an array from the response
    17. }
    18. },
    19. error: function (e) {
    20. alert(error);
    21. },
    22. requestEnd: function (e) {
    23. G.KendoDropdown.ViewModel.UpdateList(e.response);
    24. }
    25. }),
    26. Load: function () {
    27. this.List.read();
    28. },
    29. }
    30. }
    31. });
    32. })(window.Gni = window.Gni || {}, jQuery, kendo);
  3. Core: Create a JavaScript file, in my case I named it Core.Js.

    This core script is used to start the rendering of the templates and calls the view model to bind the data in the UI.
    1. $(function () {
    2. var Layout = new kendo.Layout("Layout-temp");
    3. var DashboardView = new kendo.View("Dashboard-temp", { model: Gni.KendoDropdown.ViewModel.DashboardModel });
    4. var router = new kendo.Router({
    5. init: function () {
    6. Layout.render("#Main");
    7. Layout.showIn("#content", DashboardView);
    8. }
    9. });
    10. router.start();
    11. Gni.KendoDropdown.Data.Load();
    12. });
The Result in Browser:

API Responce
Figure 3
StateList in Dropdown

Select state
Figure 4
CountryList in Kendo DropDown
Select country
Figure 5

I hope you enjoyed this article. Your valuable feedback, question, or comments about this article are always welcome.
Read more articles on ASP.NET: