MVVM

MVVM stands for Model< ->View <-> View Model which supports two-way binding between view and view model, where View-Model is responsible for exposing the data objects from the Model in such a way that those objects are easily consumed in the View.

Let us start with creating an ASP.NET WEB API

Creating an Empty ASP.NET WEB API Project:

Create a simple empty WEB API project as in the following figures:


Figure 1

Figure 2

Creating a Model Class:

Right click on the model folder 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. public class ListsitemsController : ApiController
  2. {
  3. [HttpGet]
  4. [Route("items")]
  5. public HttpResponseMessage ListItems()
  6. {
  7. List<CommonList> _list = new List<CommonList>();
  8. _list.Add(new CommonList(1, "India"));
  9. _list.Add(new CommonList(2, "China"));
  10. _list.Add(new CommonList(3, "United States"));

  11. return Request.CreateResponse(HttpStatusCode.OK, _list, Configuration.Formatters.JsonFormatter);
  12. }
  13. }

The above controller will return a Country list as a response.

Now the API is ready to consume.

Using a Kendo Dropdownlist with MVVM pattern

Creating a HTML page

Create a new HTML page in the project, in my case I named it kendoDropdownlist.html.

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/2remote-binding-of-kendo-dropdownlist-with-mvvm-pattern-using16.1.112/styles/kendo.common.min.css">
  7. <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2remote-binding-of-kendo-dropdownlist-with-mvvm-pattern-using16.1.112/styles/kendo.rtl.min.css">
  8. <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2remote-binding-of-kendo-dropdownlist-with-mvvm-pattern-using16.1.112/styles/kendo.default.min.css">
  9. <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2remote-binding-of-kendo-dropdownlist-with-mvvm-pattern-using16.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/2remote-binding-of-kendo-dropdownlist-with-mvvm-pattern-using16.1.112/js/angular.min.js"></script>
  12. <script src="http://kendo.cdn.telerik.com/2remote-binding-of-kendo-dropdownlist-with-mvvm-pattern-using16.1.112/js/jszip.min.js"></script>
  13. <script src="http://kendo.cdn.telerik.com/2remote-binding-of-kendo-dropdownlist-with-mvvm-pattern-using16.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> <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:5remote-binding-of-kendo-dropdownlist-with-mvvm-pattern-using%" /> </div>
  30. </script>
  31. </body>
  32. </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. To declare your View-Model use the kendo.observable function and pass it as a JavaScript object.

ViewModel.Js

  1. (function (G, $, undefined) {
  2. $.extend(true, G, {
  3. KendoDropdown: {
  4. ViewModel: {
  5. DashboardModel: new kendo.observable({
  6. Country: '',
  7. country: G.KendoDropdown.Data.CountryList,
  8. }),
  9. }
  10. }
  11. });
  12. })(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. CountryList: new kendo.data.DataSource({
  6. transport: {
  7. read: {
  8. type: "GET",
  9. url: "api/list/items",
  10. datatype: "json",
  11. },
  12. },
  13. schema: {
  14. model: {
  15. id: "Id",
  16. fields: {
  17. "Text": "Text",
  18. }
  19. }
  20. }
  21. }),
  22. }
  23. }
  24. });
  25. })(window.Gni = window.Gni || {}, jQuery, kendo);

1. 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. });
The Result in Browser:


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