DotNetNuke Module Development Using Kedno UI and Web API

Before starting this article please go through my previous article DotNetNuke Module Development, where I have explained how to do HTTP GET requests using Kendo Grid in DNN CMS Module development.
 
Now I will explain how to do a Web API  HTTP POST request using a Kendo form in DotNetNuke CMS Module Development.
 
In this module, I have a Kendo Grid and Kendo form. So, whatever the input is given in the Kendo form is bound into the Kendo Grid as well as it should be inserted into the respective Microsoft DB (DataBase) table that is done using the WEB API in the DNN CMS platform.
 
We will start with the back-end Microsoft SQL DB.
 
I have created a simple table with two columns, CategoryID and CategoryName.
 
Here is my SQL Query:
  1. CREATE TABLE [dbo].[dropdownkendo]  
  2. (  
  3. [CategoryID] [int] IDENTITY(1, 1) NOT NULL,  
  4. [CategoryName] [nvarchar](50) NULL,  
  5. CONSTRAINT [Pk_Category_ID] PRIMARY KEY CLUSTERED ([CategoryID] ASCWITH (  
  6. PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,  
  7. IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,  
  8. ALLOW_PAGE_LOCKS = ON  
  9. ON [PRIMARY]  
  10. ON [PRIMARY] GO Then the store procedure to insert a  
  11. values  
  12. in table Create procedure [dbo].[Sp_Insert_Kendo]   
  13. (  
  14. @CategoryName nvarchar(50)  
  15. as begin insert into dropdownkendo(CategoryName)  
  16. values  
  17. (@CategoryName) end
The next step is to open the class library project that I showed in my previous article.
 
Here I am using LINQ to SQL to access my Microsoft SQL DB, you can use whatever data access method that is convenient to you.
 
My DBML file
 
 
Figure 1 DBML File
 
Here is my API controller with Http Post request:
  1. public class InsertCategoryController: DnnApiController  
  2. {  
  3.     TestTableDataContext db = new TestTableDataContext();  
  4.     [HttpPost]  
  5.     [AllowAnonymous]  
  6.     public IEnumerable < dropdownkendo > Insert(dropdownkendo drop)  
  7.     {  
  8.         db.Sp_Insert_Kendo(drop.CategoryName);  
  9.         return db.dropdownkendos;  
  10.     }  
  11. }  
  12. public class RouteMapper: IServiceRouteMapper  
  13. {  
  14.     public void RegisterRoutes(IMapRoute mapRouteManager)  
  15.     {  
  16.         mapRouteManager.MapHttpRoute("ServicePoint""default""{controller}/{action}"new[] {  
  17.             "ServicePoint"  
  18.         });  
  19.     }  
  20. }
Here is the code to bind the table in the Kendo Grid, Note that I am reading a value from the DB to display in the Kendo Grid and that's why I am using an HTTP GET request for this action.
  1. public class DropDataController: DnnApiController  
  2. {  
  3.     TestTableDataContext db = new TestTableDataContext();  
  4.     [HttpGet]  
  5.     [AllowAnonymous]  
  6.     public IEnumerable < dropdownkendo > Drop()  
  7.     {  
  8.         return db.dropdownkendos;  
  9.     }  
  10. }
Yes, now it is time to check your API in POSTMAN/Fiddler.
 
Here I am using POSTMAN.
 
First POST request
 
 
Figure 2 Post Request
 
Next GET request
 
 
Figure 3 Get Request
 
Yes! Got it.
 
Now to consume the service.
 
Come to your web form to consume the service.
 
Here is the code:
  1. <link href="DesktopModules/Registration_Module/Content/kendo/2014.2.716/kendo.common.min.css" rel="stylesheet" />  
  2. <link href="DesktopModules/Registration_Module/Content/kendo/2014.2.716/kendo.dataviz.min.css" rel="stylesheet" />  
  3. <link href="DesktopModules/Registration_Module/Content/kendo/2014.2.716/kendo.default.min.css" rel="stylesheet" />  
  4. <link href="DesktopModules/Registration_Module/Content/kendo/2014.2.716/kendo.dataviz.default.min.css" rel="stylesheet" />  
  5. <script src="DesktopModules/Registration_Module/Scripts/kendo/2014.2.716/kendo.all.min.js"></script>  
  6. <script src="DesktopModules/Registration_Module/Scripts/kendo/2014.2.716/kendo.web.min.js"></script>  
  7. <!—Simple form -->  
  8. <label>CategoryName:  
  9.      <input data-bind="value: CategoryName" id="CategoryName" />  
  10. </label>  
  11. <button id="submit_btn" onclick="RegisterUser()" class="k-button">Submit</button>  
  12. <!-- Script to consume the API HTTPPOST Request -->  
  13. <script>  
  14. function RegisterUser() {  
  15.      // alert("hai");    
  16.      $.ajax({  
  17.           cache: false,  
  18.           async: false,  
  19.           type: "POST",  
  20.           url: "http://dnndev.me/DesktopModules/ServicePoint/api/InsertCategory/Insert",  
  21.           data: {  
  22.                CategoryName: $("#CategoryName").val()  
  23.           },  
  24.           success: function(data) {  
  25.                alert(data);  
  26.           },  
  27.      });  
  28. };  
  29. </script>  
  30. <!--kendo Grid to show the table detail -->  
  31. <div class="main-content">  
  32.      <div id="grid1"></div>  
  33. </div>  
  34. <!-- Script to consume the API HTTP GET Request-->  
  35. <script>  
  36. $(document).ready(function() {  
  37.      $("#grid1").kendoGrid({  
  38.           editable: "inline",  
  39.           columns: [{  
  40.                     field: "CategoryID",  
  41.                     title: "number",  
  42.                     editable: false  
  43.                },  
  44.                {  
  45.                     field: "CategoryName",  
  46.                     title: "Name"  
  47.                },  
  48.                {  
  49.                     command: ["edit",  
  50.                          {  
  51.                               name: "destroy",  
  52.                               text: "remove",  
  53.                          }  
  54.                     ],  
  55.                }  
  56.           ],  
  57.           dataSource: {  
  58.                transport: {  
  59.                     read: {  
  60.                          url: "http://dnndev.me:80/DesktopModules/ServicePoint/api/DropData/Drop",  
  61.                          dataType: "json",  
  62.                     },  
  63.                },  
  64.                schema: {  
  65.                     model: {  
  66.                          id: "CategoryID"  
  67.                     }  
  68.                }  
  69.           },  
  70.      });  
  71. });  
  72. </script> 
Run the Web form and the UI shows up as in the following:
 
 
Figure 4 Registration Module
 
After inserting the value:
 
 
Figure 5 Insert Value
 
My database table rows are affected as in the following:
 
 
Figure 5 Database Table
 
That's it, mission accomplished; enjoy coding!
 
I have attached my DNN module source code for your reference.


Similar Articles