ASP.NET MVC Real-Time App With SignalR

In this topic we will focus on how to display real time updates from databases with SignalR on existing ASP.NET MVC CRUD project.

The topic has the following two steps:

  1. In the first step we will create a sample app to perform CRUD operations.
  2. In the second step we will make the app real-time with SignalR.

Those who are not familiar with SignalR, visit my previous article on Overview of SignalR.

Step 1: At first we need to create a database named CRUD_Sample. In sample db we have to create a table named Customers.

  1. CREATE TABLE [dbo].[Customers](  
  2.     [Id] [bigint] IDENTITY(1,1) NOT NULL,  
  3.     [CustName] [varchar](100) NULL,  
  4.     [CustEmail] [varchar](150) NULL,  
  5.  CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED ([Id] ASC)  
  6. WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, _ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]) ON [PRIMARY]  
  7.   
  8. GO  
  9.   
  10. SET ANSI_PADDING OFF  
  11. GO  
Stored Procedures
  1. USE [CRUD_Sample]  
  2. GO  
  3.   
  4. /****** Object:  StoredProcedure [dbo].[Delete_Customer]    Script Date: 12/27/2015 1:44:05 PM ******/  
  5. SET ANSI_NULLS ON  
  6. GO  
  7.   
  8. SET QUOTED_IDENTIFIER ON  
  9. GO  
  10.   
  11. -- =============================================  
  12. -- Author:      <Author,,Name>  
  13. -- Create date: <Create Date,,>  
  14. -- Description: <Description,,>  
  15. -- =============================================  
  16. CREATE PROCEDURE [dbo].[Delete_Customer]  
  17.     -- Add the parameters for the stored procedure here  
  18.      @Id Bigint  
  19. AS  
  20. BEGIN  
  21.     -- SET NOCOUNT ON added to prevent extra result sets from  
  22.     -- interfering with SELECT statements.  
  23.     SET NOCOUNT ON;  
  24.   
  25. -- Insert statements for procedure here  
  26.     DELETE FROM [dbo].[Customers] WHERE [Id] = @Id  
  27.     SELECT 1  
  28. END  
  29.   
  30. GO  
  31.   
  32. /****** Object:  StoredProcedure [dbo].[Get_Customer]    Script Date: 12/27/2015 1:44:05 PM ******/  
  33. SET ANSI_NULLS ON  
  34. GO  
  35.   
  36. SET QUOTED_IDENTIFIER ON  
  37. GO  
  38.   
  39. -- =============================================  
  40. -- Author:      <Author,,Name>  
  41. -- Create date: <Create Date,,>  
  42. -- Description: <Description,,>  
  43. -- =============================================  
  44. CREATE PROCEDURE [dbo].[Get_Customer]   
  45.     -- Add the parameters for the stored procedure here  
  46.     @Count INT  
  47. AS  
  48. BEGIN  
  49.     -- SET NOCOUNT ON added to prevent extra result sets from  
  50.     -- interfering with SELECT statements.  
  51.     SET NOCOUNT ON;  
  52.   
  53. -- Insert statements for procedure here  
  54.     SELECT top(@Count)* FROM [dbo].[Customers]  
  55. END  
  56.   
  57. GO  
  58.   
  59. /****** Object:  StoredProcedure [dbo].[Get_CustomerbyID]    Script Date: 12/27/2015 1:44:05 PM ******/  
  60. SET ANSI_NULLS ON  
  61. GO  
  62.   
  63. SET QUOTED_IDENTIFIER ON  
  64. GO  
  65.   
  66. -- =============================================  
  67. -- Author:      <Author,,Name>  
  68. -- Create date: <Create Date,,>  
  69. -- Description: <Description,,>  
  70. -- =============================================  
  71. CREATE PROCEDURE [dbo].[Get_CustomerbyID]   
  72.     -- Add the parameters for the stored procedure here  
  73.     @Id BIGINT  
  74. AS  
  75. BEGIN  
  76.     -- SET NOCOUNT ON added to prevent extra result sets from  
  77.     -- interfering with SELECT statements.  
  78.     SET NOCOUNT ON;  
  79.   
  80. -- Insert statements for procedure here  
  81.     SELECT FROM [dbo].[Customers]  
  82.     WHERE Id=@Id  
  83. END  
  84.   
  85. GO  
  86.   
  87. /****** Object:  StoredProcedure [dbo].[Set_Customer]    Script Date: 12/27/2015 1:44:05 PM ******/  
  88. SET ANSI_NULLS ON  
  89. GO  
  90.   
  91. SET QUOTED_IDENTIFIER ON  
  92. GO  
  93.   
  94. -- =============================================  
  95. -- Author:      <Author,,Name>  
  96. -- Create date: <Create Date,,>  
  97. -- Description: <Description,,>  
  98. -- =============================================  
  99. CREATE PROCEDURE [dbo].[Set_Customer]  
  100.     -- Add the parameters for the stored procedure here  
  101.      @CustName Nvarchar(100)  
  102.     ,@CustEmail Nvarchar(150)  
  103. AS  
  104. BEGIN  
  105.     -- SET NOCOUNT ON added to prevent extra result sets from  
  106.     -- interfering with SELECT statements.  
  107.     SET NOCOUNT ON;  
  108.   
  109. -- Insert statements for procedure here  
  110.     INSERT INTO [dbo].[Customers]([CustName],[CustEmail])  
  111.     VALUES(@CustName,@CustEmail)  
  112.     SELECT 1  
  113. END  
  114.   
  115. GO  
  116.   
  117. /****** Object:  StoredProcedure [dbo].[Update_Customer]    Script Date: 12/27/2015 1:44:05 PM ******/  
  118. SET ANSI_NULLS ON  
  119. GO  
  120.   
  121. SET QUOTED_IDENTIFIER ON  
  122. GO  
  123.   
  124. -- =============================================  
  125. -- Author:      <Author,,Name>  
  126. -- Create date: <Create Date,,>  
  127. -- Description: <Description,,>  
  128. -- =============================================  
  129. CREATE PROCEDURE [dbo].[Update_Customer]  
  130.     -- Add the parameters for the stored procedure here  
  131.      @Id Bigint  
  132.     ,@CustNameNvarchar(100)  
  133.     ,@CustEmailNvarchar(150)  
  134. AS  
  135. BEGIN  
  136.     -- SET NOCOUNT ON added to prevent extra result sets from  
  137.     -- interfering with SELECT statements.  
  138.     SET NOCOUNT ON;  
  139.   
  140. -- Insert statements for procedure here  
  141.     UPDATE [dbo].[Customers] SET[CustName] = @CustName,[CustEmail]= @CustEmail  
  142.     WHERE [Id] = @Id  
  143.     SELECT 1  
  144. END  
  145.   
  146. GO  
Getting Started with MVC Project

To create a sample application, we need to have Visual Studio 2012 or later installed and be able to run the server on a platform that supports .NET 4.5.

Step 1:

Getting Started withMVC

Step 2:

Web application

Step 3:

select template

Click OK and Visual Studio will create and load a new ASP.NET application project.

Use of Generic Repository

With a generic feature, we can reduce the amount of code we need for common scenarios.
  1. namespace WebApplication1.Repository  
  2. {  
  3.     interfaceIRepository < T > : IDisposablewhereT: class  
  4.     {  
  5.         IEnumerable < T > ExecuteQuery(stringspQuery, object[] parameters);  
  6.         TExecuteQuerySingle(stringspQuery, object[] parameters);  
  7.         intExecuteCommand(stringspQuery, object[] parameters);  
  8.     }  
  9. }  
interfaceIRepository<T>

Show an interface of a generic repository of type T, which is a LINQ to SQL entity. It provides a basic interface with operations like Insert, Update, Delete, GetById and GetAll.

IDisposable

The IDisposable Interface provides a mechanism for releasing unmanaged resources.

whereT : class

This is constraining the generic parameter to a class. Click for more.

The type of argument must be a reference type; this applies also to any class, interface, delegate, or array type.
  1. namespace WebApplication1.Repository  
  2. {  
  3.     public class GenericRepository < T > : IRepository < T > whereT: class  
  4.     {  
  5.         Customer_Entities context = null;  
  6.         privateDbSet < T > entities = null;  
  7.         public GenericRepository(Customer_Entities context)  
  8.         {  
  9.             this.context = context;  
  10.             entities = context.Set < T > ();  
  11.         }  
  12.         ///<summary>  
  13.         /// Get Data From Database  
  14.         ///<para>Use it when to retive data through a stored procedure</para>  
  15.         ///</summary>  
  16.         public IEnumerable < T > ExecuteQuery(stringspQuery, object[] parameters)  
  17.         {  
  18.             using(context = newCustomer_Entities())  
  19.             {  
  20.                     returncontext.Database.SqlQuery < T > (spQuery, parameters).ToList();  
  21.             }  
  22.         }  
  23.         ///<summary>  
  24.         /// Get Single Data From Database  
  25.         ///<para>Use it when to retive single data through a stored procedure</para>  
  26.         ///</summary>  
  27.         public TExecuteQuerySingle(stringspQuery, object[] parameters)  
  28.         {  
  29.             using(context = newCustomer_Entities())  
  30.             {  
  31.                 returncontext.Database.SqlQuery < T > (spQuery, parameters).FirstOrDefault();  
  32.             }  
  33.         }  
  34.         ///<summary>  
  35.         /// Insert/Update/Delete Data To Database  
  36.         ///<para>Use it when to Insert/Update/Delete data through a stored procedure</para>  
  37.         ///</summary>  
  38.         public intExecuteCommand(stringspQuery, object[] parameters)  
  39.         {  
  40.             int result = 0;  
  41.             try  
  42.             {  
  43.                 using(context = newCustomer_Entities())  
  44.                 {  
  45.                     result = context.Database.SqlQuery < int > (spQuery, parameters).FirstOrDefault();  
  46.                 }  
  47.             }  
  48.             catch  
  49.             {}  
  50.             return result;  
  51.         }  
  52.         private bool disposed = false;  
  53.         protected virtualvoid Dispose(bool disposing)  
  54.         {  
  55.             if (!this.disposed)  
  56.             {  
  57.                 if (disposing)  
  58.                 {  
  59.                     context.Dispose();  
  60.                 }  
  61.             }  
  62.             this.disposed = true;  
  63.         }  
  64.         public void Dispose()  
  65.         {  
  66.             Dispose(true);  
  67.             GC.SuppressFinalize(this);  
  68.         }  
  69.     }  
  70. }  
Use of middle-tire
  1. namespace WebApplication1.Services  
  2. {  
  3.     public partial class CustomerService  
  4.     {  
  5.         privateGenericRepository < Customer > CustRepository;  
  6.         //CustomerRepositoryCustRepository;  
  7.         public CustomerService()  
  8.         {  
  9.             this.CustRepository = newGenericRepository < Customer > (newCustomer_Entities());  
  10.         }  
  11.         public IEnumerable < Customer > GetAll(object[] parameters)  
  12.         {  
  13.             stringspQuery = "[Get_Customer] {0}";  
  14.             returnCustRepository.ExecuteQuery(spQuery, parameters);  
  15.         }  
  16.         public CustomerGetbyID(object[] parameters)  
  17.         {  
  18.             stringspQuery = "[Get_CustomerbyID] {0}";  
  19.             returnCustRepository.ExecuteQuerySingle(spQuery, parameters);  
  20.         }  
  21.         public int Insert(object[] parameters)  
  22.         {  
  23.             stringspQuery = "[Set_Customer] {0}, {1}";  
  24.             returnCustRepository.ExecuteCommand(spQuery, parameters);  
  25.         }  
  26.         public int Update(object[] parameters)  
  27.         {  
  28.             stringspQuery = "[Update_Customer] {0}, {1}, {2}";  
  29.             returnCustRepository.ExecuteCommand(spQuery, parameters);  
  30.         }  
  31.         public int Delete(object[] parameters)  
  32.         {  
  33.             stringspQuery = "[Delete_Customer] {0}";  
  34.             returnCustRepository.ExecuteCommand(spQuery, parameters);  
  35.         }  
  36.     }  
  37. }  
Use of Generic Repository in MVC-Application:
  1. namespace WebApplication1.Controllers  
  2. {  
  3.     public class HomeController: Controller  
  4.     {  
  5.         private CustomerServiceobjCust;  
  6.         //CustomerRepositoryCustRepository;  
  7.         public HomeController()  
  8.             {  
  9.                 this.objCust = newCustomerService();  
  10.             }  
  11.             // GET: Home  
  12.         public ActionResult Index()  
  13.         {  
  14.             int Count = 10;  
  15.             object[] parameters = {  
  16.                 Count  
  17.             };  
  18.             var test = objCust.GetAll(parameters);  
  19.             return View(test);  
  20.         }  
  21.         public ActionResult Insert()  
  22.         {  
  23.             return View();  
  24.         }  
  25.         [HttpPost]  
  26.         public ActionResult Insert(Customer model)  
  27.         {  
  28.             if (ModelState.IsValid)  
  29.             {  
  30.                 object[] parameters = {  
  31.                     model.CustName,  
  32.                     model.CustEmail  
  33.                 };  
  34.                 objCust.Insert(parameters);  
  35.             }  
  36.             return RedirectToAction("Index");  
  37.         }  
  38.         public ActionResult Delete(int id)  
  39.         {  
  40.             object[] parameters = {  
  41.                 id  
  42.             };  
  43.             this.objCust.Delete(parameters);  
  44.             return RedirectToAction("Index");  
  45.         }  
  46.         public ActionResult Update(int id)  
  47.         {  
  48.             object[] parameters = {  
  49.                 id  
  50.             };  
  51.             return View(this.objCust.GetbyID(parameters));  
  52.         }  
  53.         [HttpPost]  
  54.         public ActionResult Update(Customer model)  
  55.         {  
  56.             object[] parameters = {  
  57.                 model.Id,  
  58.                 model.CustName,  
  59.                 model.CustEmail  
  60.             };  
  61.             objCust.Update(parameters);  
  62.             return RedirectToAction("Index");  
  63.         }  
  64.         protected override void Dispose(bool disposing)  
  65.         {  
  66.             base.Dispose(disposing);  
  67.         }  
  68.     }  
  69. }  
Use of views in MVC-Application

Index
  1. @model IList  
  2. <WebApplication1.Models.Customer>  
  3. @{  
  4. ViewBag.Title = "Index";  
  5. }  
  6.   
  7.     <linkhref="~/Content/bootstrap/css/bootstrap.min.css"rel="stylesheet"/>  
  8.     <divclass="clearfix">   
  9.     </div>  
  10.     <divclass="clearfix">   
  11.     </div>  
  12.     <divclass="container">  
  13.         <divclass="table-responsive">  
  14. @Html.ActionLink("New Customer""Insert""Home")  
  15.   
  16.             <tableclass="table table-bordered table-striped">  
  17.                 <thead>  
  18.                     <tr>  
  19.                         <th>ID</th>  
  20.                         <th>Name</th>  
  21.                         <th>Email ID</th>  
  22.                         <th>Delete</th>  
  23.                         <th>Update</th>  
  24.                     </tr>  
  25.                 </thead>  
  26.                 <tbody>  
  27.                 @if (Model != null)  
  28.                 {  
  29.                     foreach (var item in Model)  
  30.                     {  
  31.   
  32.                        <tr>  
  33.                            <td>@item.Id</td>  
  34.                            <td>@item.CustName</td>  
  35.                            <td>@item.CustEmail</td>  
  36.                            <td>@Html.ActionLink("Delete""Delete""Home"new { id = @item.Id }, null)</td>  
  37.                            <td>@Html.ActionLink("Update""Update""Home"new { id = @item.Id }, null)</td>  
  38.                        </tr>  
  39.                     }  
  40.                 }  
  41.   
  42.                 </tbody>  
  43.             </table>  
  44.         </div>  
  45.         <divclass="clearfix">   
  46.         </div>  
  47.     </div>  
Insert
  1. @model WebApplication1.Models.Customer  
  2. @{  
  3. ViewBag.Title = "Insert";  
  4. }  
  5.   
  6. <link href="~/Content/bootstrap/css/bootstrap.min.css"rel="stylesheet"/>  
  7. <div class="clearfix">   
  8. </div>  
  9. <div class="clearfix">   
  10. </div>  
  11. <div class="container">  
  12.     <div class="table-responsive col-md-6 col-md-offset-3">  
  13.         <table class="table table-bordered table-striped">  
  14.             <tbody>  
  15. @using (Html.BeginForm("Insert""Home", FormMethod.Post))  
  16. {  
  17. @*  
  18.                 <tr>  
  19.                     <td class="col-md-4">ID</td>  
  20.                     <td class="col-md-8">@Html.TextBoxFor(m =>m.Id)</td>  
  21.                 </tr>*@  
  22.   
  23.                 <tr>  
  24.                     <td class="col-md-4">Name  
  25.                     </td>  
  26.                     <td class="col-md-8">@Html.TextBoxFor(m =>m.CustName)  
  27.                     </td>  
  28.                 </tr>  
  29.                 <tr>  
  30.                     <td class="col-md-4">Email ID  
  31.                     </td>  
  32.                     <td class="col-md-8">@Html.TextBoxFor(m =>m.CustEmail)  
  33.                     </td>  
  34.                 </tr>  
  35.                 <tr>  
  36.                     <td class="text-right"colspan="2">  
  37.                         <input type="submit"value="Save"class="btnbtn-primary"/>  
  38.                     </td>  
  39.                 </tr>  
  40. }  
  41.   
  42.             </tbody>  
  43.         </table>  
  44.     </div>  
  45.     <div class="clearfix">   
  46.     </div>  
  47. @Html.ActionLink("Home""Index""Home")  
  48.   
  49. </div>  
Update
  1. @model WebApplication1.Models.Customer  
  2. @{  
  3. ViewBag.Title = "Update";  
  4. }  
  5.   
  6.   
  7. <link href="~/Content/bootstrap/css/bootstrap.min.css"rel="stylesheet"/>  
  8. <div class="clearfix">   
  9. </div>  
  10. <div class="clearfix">   
  11. </div>  
  12. <div class="container">  
  13.     <div class="table-responsive">  
  14.         <table class="table table-bordered table-striped">  
  15.             <thead>  
  16.                 <tr>  
  17.                     <th>Name</th>  
  18.                     <th>Email ID</th>  
  19.                     <th>Update</th>  
  20.                 </tr>  
  21.             </thead>  
  22.             <tbody>  
  23.                 <tr>  
  24. @using (Html.BeginForm("Update""Home", FormMethod.Post))  
  25. {  
  26.   
  27.                     <td>@Html.TextBoxFor(m =>m.CustName)</td>  
  28.                     <td>@Html.TextBoxFor(m =>m.CustEmail)</td>  
  29.                     <td>  
  30.                         <inputtype="submit"value="Update"class="btnbtn-primary"/>  
  31.                     </td>  
  32. }  
  33.   
  34.                 </tr>  
  35.             </tbody>  
  36.         </table>  
  37.     </div>  
  38. </div>  
Step 2:

Getting Started with SignalR


The first thing is getting a reference from NuGet.

Get it on NuGet!

Install-Package Microsoft.AspNet.SignalR

Install-Package

Register SignalR middleware

Once you have installed it let’s create OwinStartup Class.

The following code adds a simple piece of middleware to the OWIN pipeline, implemented as a function that receives a Microsoft.Owin.IOwinContext instance.

When the server receives an HTTP request, the OWIN pipeline invokes the middleware. The middleware sets the content type for the response and writes the response body.

Startup.cs
  1. using System;  
  2. using System.Threading.Tasks;  
  3. using Microsoft.Owin;  
  4. using Owin;  
  5. [assembly: OwinStartup(typeof (WebAppSignalR.Startup))]  
  6. namespace WebAppSignalR  
  7. {  
  8.     public class Startup  
  9.     {  
  10.         public void Configuration(IAppBuilder app)  
  11.         {  
  12.             app.MapSignalR();  
  13.         }  
  14.     }  
  15. }  
Create &Use Hub classes

After finishing the previous process, let’s create a Hub. A SignalR Hub make remote procedure calls (RPCs) from a server to connected clients and from clients to the server.

CustomerHub.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using Microsoft.AspNet.SignalR;  
  6. using Microsoft.AspNet.SignalR.Hubs;  
  7. namespace WebApplication1.Hubs  
  8. {  
  9.     public class CustomerHub: Hub  
  10.     {  
  11.         [HubMethodName("broadcastData")]  
  12.         public static void BroadcastData()  
  13.         {  
  14.             IHubContext context = GlobalHost.ConnectionManager.GetHubContext < CustomerHub > ();  
  15.             context.Clients.All.updatedData();  
  16.         }  
  17.     }  
  18. }  
Code Explanation
  1. IHubContext context = GlobalHost.ConnectionManager.GetHubContext<CustomerHub>();  
It gets the CustomerHub context:
  1. context.Clients.All.updatedData();  
It call the client part of SignalR and tell it to execute the JavaScript method updatedData().

Let’s Modify our Existing View

Now we will modify part of Index view as in the following, and we will display data with a partial view.

Index

  1. @model IList < WebApplication1.Models.Customer > @  
  2. {  
  3.     ViewBag.Title = "Index";  
  4. } < linkhref = "~/Content/bootstrap/css/bootstrap.min.css"  
  5. rel = "stylesheet" / > < divclass = "clearfix" > & nbsp; < /div> < divclass = "clearfix" > & nbsp; < /div> < divclass = "container" > < divclass = "table-responsive" > @Html.ActionLink("New Customer""Insert""Home") < hr / > < divid = "dataTable" > < /div> < /div> < divclass = "clearfix" > & nbsp; < /div> < /div>  
  6. @section JavaScript  
  7. { < scriptsrc = "~/Scripts/jquery.signalR-2.2.0.min.js" > < /script> < scriptsrc = "/signalr/hubs" > < /script> < scripttype = "text/javascript" > $(function ()  
  8.     {  
  9.         // Reference the hub.  
  10.         var hubNotif = $.connection.customerHub;  
  11.         // Start the connection.  
  12.         $.connection.hub.start().done(function ()  
  13.         {  
  14.             getAll();  
  15.         });  
  16.         // Notify while anyChanges.  
  17.         hubNotif.client.updatedData = function ()  
  18.         {  
  19.             getAll();  
  20.         };  
  21.     });  
  22.     function getAll()  
  23.     {  
  24.         var model = $('#dataTable');  
  25.         $.ajax(  
  26.         {  
  27.             url: '/home/GetAllData',  
  28.             contentType: 'application/html ; charset:utf-8',  
  29.             type: 'GET',  
  30.             dataType: 'html'  
  31.         }).success(function (result)  
  32.         {  
  33.             model.empty().append(result);  
  34.         }).error(function (e)  
  35.         {  
  36.             alert(e);  
  37.         });  
  38.     } < /script>  
  39. }  
Partial View
  1. <table class="table table-bordered table-striped">  
  2.     <thead>  
  3.         <tr>  
  4.             <th>ID</th>  
  5.             <th>Name</th>  
  6.             <th>Email ID</th>  
  7.             <th>Delete</th>  
  8.             <th>Update</th>  
  9.         </tr>  
  10.     </thead>  
  11.     <tbody> @if (Model != null) { foreach (var item in Model) {  
  12.         <tr>  
  13.             <td>@item.Id</td>  
  14.             <td>@item.CustName</td>  
  15.             <td>@item.CustEmail</td>  
  16.             <td>@Html.ActionLink("Delete""Delete""Home"new { id = @item.Id }, null)</td>  
  17.             <td>@Html.ActionLink("Update""Update""Home"new { id = @item.Id }, null)</td>  
  18.         </tr> } } </tbody>  
  19.     </table>  
Let’s Modify our Existing Controller

Home Controller:

In our home controller we will add a method named GetAllData(). Here's the method.
  1. [HttpGet]  
  2. public ActionResult GetAllData()  
  3. {  
  4.     int Count = 10;  
  5.     object[] parameters = {  
  6.         Count  
  7.     };  
  8.     var test = objCust.GetAll(parameters);  
  9.     return PartialView("_DataList", test);  
  10. }  
Here we are returning a partial view with returned data list, and just returning empty.
  1. // GET: Home  
  2. public ActionResult Index()  
  3. {  
  4.    return View();  
  5. }  
Home Controller
  1. public class HomeController: Controller  
  2. {  
  3.     private CustomerService objCust;  
  4.     //CustomerRepositoryCustRepository;  
  5.     public HomeController()  
  6.     {  
  7.         this.objCust = newCustomerService();  
  8.     }  
  9.     // GET: Home  
  10.     public ActionResult Index()  
  11.     {  
  12.         return View();  
  13.     }  
  14.     [HttpGet]  
  15.     public ActionResult GetAllData()  
  16.     {  
  17.         int Count = 10;  
  18.         object[] parameters = {  
  19.             Count  
  20.         };  
  21.         var test = objCust.GetAll(parameters);  
  22.         return PartialView("_DataList", test);  
  23.     }  
  24.     public ActionResult Insert()  
  25.     {  
  26.         return View();  
  27.     }  
  28.     [HttpPost]  
  29.     public ActionResult Insert(Customer model)  
  30.     {  
  31.         if (ModelState.IsValid)  
  32.         {  
  33.             object[] parameters = {  
  34.                 model.CustName,  
  35.                 model.CustEmail  
  36.             };  
  37.             objCust.Insert(parameters);  
  38.         }  
  39.         //Notify to all  
  40.         CustomerHub.BroadcastData();  
  41.         return RedirectToAction("Index");  
  42.     }  
  43.     public ActionResult Delete(int id)  
  44.     {  
  45.         object[] parameters = {  
  46.             id  
  47.         };  
  48.         this.objCust.Delete(parameters);  
  49.         //Notify to all  
  50.         CustomerHub.BroadcastData();  
  51.         return RedirectToAction("Index");  
  52.     }  
  53.     public ActionResult Update(int id)  
  54.     {  
  55.         object[] parameters = {  
  56.             id  
  57.         };  
  58.         return View(this.objCust.GetbyID(parameters));  
  59.     }  
  60.     [HttpPost]  
  61.     public ActionResult Update(Customer model)  
  62.     {  
  63.         object[] parameters = {  
  64.             model.Id,  
  65.             model.CustName,  
  66.             model.CustEmail  
  67.         };  
  68.         objCust.Update(parameters);  
  69.         //Notify to all  
  70.         CustomerHub.BroadcastData();  
  71.         returnRedirectToAction("Index");  
  72.     }  
  73.     protected override void Dispose(bool disposing)  
  74.     {  
  75.         base.Dispose(disposing);  
  76.     }  
  77. }  
Output

run

I hope this will help someone.


Similar Articles