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
- public class CommonList
- {
- public CommonList(int Id, string Text)
- {
- this.Id = Id;
- this.Text = Text;
- }
- public int Id { get; set; }
- public string Text { get; set; }
- }
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
- [RoutePrefix("api/list")]
- public class ListsitemsController : ApiController
- {
- [HttpGet]
- [Route("items")]
- public HttpResponseMessage ListItems()
- {
- List<CommonList> _list = new List<CommonList>();
- _list.Add(new CommonList(1, "India"));
- _list.Add(new CommonList(2, "China"));
- _list.Add(new CommonList(3, "United States"));
-
-
- List<CommonList> _list1 = new List<CommonList>();
- _list1.Add(new CommonList(1, "TamilNadu"));
- _list1.Add(new CommonList(2, "Karnataka"));
- _list1.Add(new CommonList(3, "NewYork"));
-
- return this.JsonResponse(
- new
- {
- CountryList = _list,
- StateList = _list1,
- });
-
- }
-
-
- public HttpResponseMessage JsonResponse(object response, HttpStatusCode httpStatusCode = HttpStatusCode.OK)
- {
- return Request.CreateResponse(httpStatusCode, response, Configuration.Formatters.JsonFormatter);
- }
- }
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.
- <!DOCTYPE html>
- <html>
- <head>
- <title>Kendo Dropdownlist MVVM</title>
- <meta charset="utf-8" />
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.112/styles/kendo.common.min.css">
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.112/styles/kendo.rtl.min.css">
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.112/styles/kendo.default.min.css">
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.1.112/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.112/js/angular.min.js"></script>
- <script src="http://kendo.cdn.telerik.com/2016.1.112/js/jszip.min.js"></script>
- <script src="http://kendo.cdn.telerik.com/2016.1.112/js/kendo.all.min.js"></script>
-
- <script src="scripts/Core.js"></script>
- <script src="scripts/Data.js"></script>
- <script src="scripts/ViewModel.js"></script>
- </head>
- <body>
- <div class="col-lg-12">
- <div id="Main" class="main"></div>
- </div>
-
- <script type="text/x-kendo-template" id="Layout-temp">
- <div class="col-lg-12">
- <div id="content"></div>
- </div>
- </script>
- <script type="text/x-kendo-template" id="Dashboard-temp">
- <div class="row">
- <h4>Kendo DropDownlist with MVVM Pattern</h4>
- <input data-role="dropdownlist"
- data-bind="source:state,value:State"
- data-text-field="Text" data-value-field="Id" data-option-label="--Select State--"
- style="width:50%" />
- <br />
- <br />
- <br />
- <input data-role="dropdownlist"
- data-bind="source:country,value:Country"
- data-text-field="Text" data-value-field="Id" data-option-label="--Select Country--"
- style="width:50%" />
- </div>
- </script>
-
- </body>
- </html>
Creating a JavaScript files
- 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
- (function (G, $, undefined) {
-
-
- $.extend(true, G, {
- KendoDropdown: {
- ViewModel: {
- DashboardModel: new kendo.observable({
- Country: '',
- country: [],
- state: [],
- State: '',
-
-
- }),
- UpdateList: function (resp) {
- this.DashboardModel.set('country', resp.CountryList);
- this.DashboardModel.set('state', resp.StateList)
- },
- }
- }
- });
- })(window.Gni = window.Gni || {}, jQuery);
- 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
- (function (G, $, K, undefined) {
-
- $.extend(true, G, {
- KendoDropdown: {
- Data: {
- List: new K.data.DataSource({
- data: {},
- transport: {
- read: {
- url: 'api/list/items',
- type: 'get',
- dataType: 'json',
-
- }
- },
- schema: {
- data: function (response) {
- return [response];
- }
- },
- error: function (e) {
-
- alert(error);
- },
- requestEnd: function (e) {
- G.KendoDropdown.ViewModel.UpdateList(e.response);
- }
- }),
- Load: function () {
- this.List.read();
-
- },
- }
- }
- });
- })(window.Gni = window.Gni || {}, jQuery, kendo);
- 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.
- $(function () {
- var Layout = new kendo.Layout("Layout-temp");
- var DashboardView = new kendo.View("Dashboard-temp", { model: Gni.KendoDropdown.ViewModel.DashboardModel });
- var router = new kendo.Router({
- init: function () {
- Layout.render("#Main");
- Layout.showIn("#content", DashboardView);
-
- }
- });
- router.start();
- Gni.KendoDropdown.Data.Load();
- });
The Result in Browser:
Figure 3
StateList in Dropdown
Figure 4
CountryList in Kendo DropDown
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: