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 Gird, Note that I am reading a value from the DB to display in the Kendo Grid and that's why I am using a 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. {  
  16. // alert("hai");  
  17. $.ajax(  
  18. {  
  19. cache: false,  
  20. async: false,  
  21. type: "POST",  
  22. url: "http://dnndev.me/DesktopModules/ServicePoint/api/InsertCategory/Insert",  
  23. data:  
  24. {  
  25. CategoryName: $("#CategoryName").val()  
  26. },  
  27. success: function (data) {  
  28. alert(data);  
  29. },  
  30. });  
  31. };  
  32. </script>  
  33. <!--kendo Grid to show the table detail -->  
  34. <div class="main-content">  
  35.     <div id="grid1"></div>  
  36. </div>  
  37. <!-- Script to consume the API HTTP GET Request-->  
  38. <script>  
  39. $(document).ready(function (){  
  40. $("#grid1").kendoGrid({  
  41. editable: "inline",  
  42. columns: [  
  43. {  
  44. field: "CategoryID",  
  45. title: "number",  
  46. editable: false  
  47. },  
  48. {  
  49. field: "CategoryName",  
  50. title: "Name"  
  51. },  
  52. {  
  53. command: ["edit",  
  54. {  
  55. name: "destroy",  
  56. text: "remove",  
  57. }  
  58. ],  
  59. }  
  60. ],  
  61. dataSource:  
  62. {  
  63. transport:{  
  64. read:  
  65. {  
  66. url:"http://dnndev.me:80/DesktopModules/ServicePoint/api/DropData/Drop",  
  67. dataType: "json",  
  68. },  
  69. },  
  70. schema:  
  71. {  
  72. model:  
  73. {  
  74. id:"CategoryID"  
  75. }  
  76. }  
  77. },  
  78. });  
  79. });  
  80. </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.

Up Next
    Ebook Download
    View all
    Learn
    View all