I am using Microsoft Enterprise Library to perform the database operation. Microsoft provides this library to perform the database operations which is very flexible to use with the web application.

So, let’s start with the following procedure:

Prerequisites

There are the following prerequisite before getting started with the scenario,

Note: I am creating this project in Visual Studio 2015.

Creating Solution

In this section we will create a blank solution and in the blank solution we will categorize the project into different categories as “Web”, “Models” and “Infrastructure”. In the Web folder we will create the web application with the help of ASP.NET MVC 5 Project Template and in the rest two solution folders we will create the “Class Library” for Models and “Core” for database operations. So let’s start with the following steps:

Step 1: Open Visual Studio 2015 and click on “New Project”,

Visual Studio Start Page
Figure 1: Visual Studio Start Page

Step 2:
Select Visual Studio Solutions from the left pane and select the “Blank Solution” named “CricketMasters”.

Creating Blank Solution
Figure 2: Creating Blank Solution

Step 3: Right click on the “CricketMasters” from the Solution Explorer and add a “New Solution Folder” named “Web”,

Adding New Solution Folder
Figure 3: Adding New Solution Folder

Create two more solutions folder named “Infrastructure” and “Models”.

Step 4: Right click on the “Web” folder and select “New” and click “Add New Project”,

Adding New Project In Solution
Figure 4: Adding New Project In Solution

Step 5:
Select the Web from the left pane in the next wizard and click on the “ASP.NET Web Application” to create web application named “CricketMastersWeb

Adding Web Application Project
Figure 5: Adding Web Application Project

Step 6: In the next “ASP.NET” wizard select the “MVC Project Template” to create ASP.NET MVC Application.

MVC Project Template
Figure 6: MVC Project Template

Now your web project created successfully.

Perform Database Operation

In this section we will create the database for the application and tables with the records. We will create stored procedures for getting records from the database. Begin with the following steps:

Step 1:
Create a database with the following query:

  1. CREATE DATABASE Cricketer
Step 2: Now, let’s execute the following SQL Script for creating table with the records and stored procedures:
  1. USE [Cricketer]
  2. GO
  3. /****** Object: Table [dbo].[CricketerProfile] Script Date: 12/13/2015 1:33:41 PM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. SET ANSI_PADDING ON
  9. GO
  10. IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CricketerProfile]') AND type in (N'U'))
  11. BEGIN
  12. CREATE TABLE [dbo].[CricketerProfile](
  13. [ID] [int] IDENTITY(1,1) NOT NULL,
  14. [Name] [varchar](50) NULL,
  15. [ODI] [int] NULL,
  16. [Tests] [int] NULL,
  17. [ODIRuns] [int] NULL,
  18. [TestRuns] [int] NULL,
  19. CONSTRAINT [PK_CricketerProfile] PRIMARY KEY CLUSTERED
  20. (
  21. [ID] ASC
  22. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
  23. ) ON [PRIMARY]
  24. END
  25. GO
  26. SET ANSI_PADDING OFF
  27. GO
  28. SET IDENTITY_INSERT [dbo].[CricketerProfile] ON
  29. GO
  30. INSERT [dbo].[CricketerProfile] ([ID], [Name], [ODI], [Tests], [ODIRuns], [TestRuns]) VALUES (1, N'Sachin Tendulkar', 463, 200, 18426, 15921)
  31. GO
  32. INSERT [dbo].[CricketerProfile] ([ID], [Name], [ODI], [Tests], [ODIRuns], [TestRuns]) VALUES (2, N'Saurav Ganguly', 311, 113, 11363, 7212)
  33. GO
  34. INSERT [dbo].[CricketerProfile] ([ID], [Name], [ODI], [Tests], [ODIRuns], [TestRuns]) VALUES (3, N'Rahul Dravid', 344, 164, 10889, 13228)
  35. GO
  36. INSERT [dbo].[CricketerProfile] ([ID], [Name], [ODI], [Tests], [ODIRuns], [TestRuns]) VALUES (4, N'V.V.S. Laxman', 86, 134, 2338, 8781)
  37. GO
  38. INSERT [dbo].[CricketerProfile] ([ID], [Name], [ODI], [Tests], [ODIRuns], [TestRuns]) VALUES (5, N'Virendar Sehwag', 251, 104, 8273, 8586)
  39. GO
  40. SET IDENTITY_INSERT [dbo].[CricketerProfile] OFF
  41. GO
  42. /****** Object: StoredProcedure [dbo].[CC_GetCricketerDetailsById] Script Date: 12/13/2015 1:33:41 PM ******/
  43. SET ANSI_NULLS ON
  44. GO
  45. SET QUOTED_IDENTIFIER ON
  46. GO
  47. IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CC_GetCricketerDetailsById]') AND type in (N'P', N'PC'))
  48. BEGIN
  49. EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[CC_GetCricketerDetailsById] AS'
  50. END
  51. GO
  52. ALTER Proc [dbo].[CC_GetCricketerDetailsById]
  53. @ID int
  54. AS
  55. Begin
  56. select * from CricketerProfile (NOLOCK) where ID = @Id
  57. End
  58. GO
  59. /****** Object: StoredProcedure [dbo].[CC_GetCricketerList] Script Date: 12/13/2015 1:33:41 PM ******/
  60. SET ANSI_NULLS ON
  61. GO
  62. SET QUOTED_IDENTIFIER ON
  63. GO
  64. IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CC_GetCricketerList]') AND type in (N'P', N'PC'))
  65. BEGIN
  66. EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[CC_GetCricketerList] AS'
  67. END
  68. GO
  69. ALTER Proc [dbo].[CC_GetCricketerList]
  70. AS
  71. Begin
  72. select ID,Name from CricketerProfile (NOLOCK)
  73. End
  74. GO
Working with Microsoft Enterprise Library

In this section we will install the Microsoft Enterprise Library from the Manage NuGet Packages in the “CricketMasters.Core” project and create the class library for the web application. So, let’s start with the following steps:

Step 1: In the Solution Explorer, right click on the Models and add a “Class Library Project” by clicking on “New Project” in the Add sub menu as “CricketMasters.Models

Adding Class Library Project
Figure 7: Adding Class Library Project

Step 2: Add a new class in the Models project.

Adding New Class in Project
Figure 8: Adding New Class in Project

Step 3: Replace the class with the following code of class:
  1. namespace CricketMasters.Models
  2. {
  3. #region Cricketer Class
  4. /// <summary>
  5. /// This class is used for the cricketers
  6. /// </summary>
  7. public class Cricketer
  8. {
  9. #region Properties
  10. /// <summary>
  11. /// get and set the ID
  12. /// </summary>
  13. public int ID { get; set; }
  14. /// <summary>
  15. /// get and set the Name
  16. /// </summary>
  17. public string Name { get; set; }
  18. /// <summary>
  19. /// get and set the ODI
  20. /// </summary>
  21. public int ODI { get; set; }
  22. /// <summary>
  23. /// get and set the Tests
  24. /// </summary>
  25. public int Tests { get; set; }
  26. /// <summary>
  27. /// get and set the OdiRuns
  28. /// </summary>
  29. public int OdiRuns { get; set; }
  30. /// <summary>
  31. /// get and set the TestRuns
  32. /// </summary>
  33. public int TestRuns { get; set; }
  34. /// <summary>
  35. /// get and set the Cricketers
  36. /// </summary>
  37. public List<Cricketer> Cricketers { get; set; }
  38. #endregion
  39. }
  40. #endregion
  41. }
Note: Build the solution.

Step 4: In the Solution Explorer, right click on the Infrastructure folder and add a “New Project” named “CricketMasters.Core”.

Step 5: Right click on the project in the Infrastructure folder and add two new folders names“BLL” and “DAL”.

Adding New Folder in Project
Figure 9: Adding New Folder in Project

Step 6: Right click on the “References” in the core project and add a reference of the Models project.

Adding Model Reference
Figure 10: Adding Model Reference

Step 7: Right click on the core project and click on “Manage NuGet Packages

Adding NuGet Package
Figure 11: Adding NuGet Package

Step 8: Search “Enterprise Library” and install the library,

Adding Microsoft Enterprise Library
Figure 12: Adding Microsoft Enterprise Library

Step 9: Add a class in the DAL folder.

Adding New Class in Folder
Figure 13: Adding New Class in Folder

Step 10: Replace the DAL class code with the following code:
  1. using CricketMasters.Models;
  2. using Microsoft.Practices.EnterpriseLibrary.Data;
  3. using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Configuration;
  7. using System.Data;
  8. using System.Data.Common;
  9. using System.Linq;
  10. using System.Reflection;
  11. namespace CricketMasters.Core.DAL
  12. {
  13. #region Cricketer DAL
  14. /// <summary>
  15. /// This class is used for Cricketer Data Access Class
  16. /// </summary>
  17. public class CricketerDAL
  18. {
  19. #region Variable
  20. /// <summary>
  21. /// Specify the Database variable
  22. /// </summary>
  23. Database objDB;
  24. /// <summary>
  25. /// Specify the static variable
  26. /// </summary>
  27. static string ConnectionString;
  28. #endregion
  29. #region Constructor
  30. /// <summary>
  31. /// This constructor is used to get the connectionstring from the config file
  32. /// </summary>
  33. public CricketerDAL()
  34. {
  35. ConnectionString = ConfigurationManager.ConnectionStrings["CricketerConnectionString"].ToString();
  36. }
  37. #endregion
  38. #region Database Method
  39. public List<T> ConvertTo<T>(DataTable datatable) where T : new()
  40. {
  41. List<T> Temp = new List<T>();
  42. try
  43. {
  44. List<string> columnsNames = new List<string>();
  45. foreach (DataColumn DataColumn in datatable.Columns)
  46. columnsNames.Add(DataColumn.ColumnName);
  47. Temp = datatable.AsEnumerable().ToList().ConvertAll<T>(row => getObject<T>(row, columnsNames));
  48. return Temp;
  49. }
  50. catch
  51. {
  52. return Temp;
  53. }
  54. }
  55. public T getObject<T>(DataRow row, List<string> columnsName) where T : new()
  56. {
  57. T obj = new T();
  58. try
  59. {
  60. string columnname = "";
  61. string value = "";
  62. PropertyInfo[] Properties;
  63. Properties = typeof(T).GetProperties();
  64. foreach (PropertyInfo objProperty in Properties)
  65. {
  66. columnname = columnsName.Find(name => name.ToLower() == objProperty.Name.ToLower());
  67. if (!string.IsNullOrEmpty(columnname))
  68. {
  69. value = row[columnname].ToString();
  70. if (!string.IsNullOrEmpty(value))
  71. {
  72. if (Nullable.GetUnderlyingType(objProperty.PropertyType) != null)
  73. {
  74. value = row[columnname].ToString().Replace("$", "").Replace(",", "");
  75. objProperty.SetValue(obj, Convert.ChangeType(value, Type.GetType(Nullable.GetUnderlyingType(objProperty.PropertyType).ToString())), null);
  76. }
  77. else
  78. {
  79. value = row[columnname].ToString();
  80. objProperty.SetValue(obj, Convert.ChangeType(value, Type.GetType(objProperty.PropertyType.ToString())), null);
  81. }
  82. }
  83. }
  84. }
  85. return obj;
  86. }
  87. catch (Exception ex)
  88. {
  89. return obj;
  90. }
  91. }
  92. #endregion
  93. #region College
  94. /// <summary>
  95. /// This method is used to get the cricketer data
  96. /// </summary>
  97. /// <returns></returns>
  98. public List<Cricketer> GetCricketerList()
  99. {
  100. List<Cricketer> objGetCricketers = null;
  101. objDB = new SqlDatabase(ConnectionString);
  102. using (DbCommand objcmd = objDB.GetStoredProcCommand("CC_GetCricketerList"))
  103. {
  104. try
  105. {
  106. using (DataTable dataTable = objDB.ExecuteDataSet(objcmd).Tables[0])
  107. {
  108. objGetCricketers = ConvertTo<Cricketer>(dataTable);
  109. }
  110. }
  111. catch (Exception ex)
  112. {
  113. throw ex;
  114. return null;
  115. }
  116. }
  117. return objGetCricketers;
  118. }
  119. /// <summary>
  120. /// This method is used to get cricketers details by cricketer id
  121. /// </summary>
  122. /// <returns></returns>
  123. public List<Cricketer> GetCricketerDetailsById(int Id)
  124. {
  125. List<Cricketer> objCricketerDetails = null;
  126. objDB = new SqlDatabase(ConnectionString);
  127. using (DbCommand objcmd = objDB.GetStoredProcCommand("CC_GetCricketerDetailsById"))
  128. {
  129. try
  130. {
  131. objDB.AddInParameter(objcmd, "@ID", DbType.Int32, Id);
  132. using (DataTable dataTable = objDB.ExecuteDataSet(objcmd).Tables[0])
  133. {
  134. objCricketerDetails = ConvertTo<Cricketer>(dataTable);
  135. }
  136. }
  137. catch (Exception ex)
  138. {
  139. throw ex;
  140. return null;
  141. }
  142. }
  143. return objCricketerDetails;
  144. }
  145. #endregion
  146. }
  147. #endregion
  148. }
Step 11: Now add a class in the BLL folder named “CricketerBL” and add the following code in the class:
  1. using CricketMasters.Core.DAL;
  2. using CricketMasters.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace CricketMasters.Core.BLL
  6. {
  7. public class CricketerBL
  8. {
  9. public List<Cricketer> GetCricketerList()
  10. {
  11. List<Cricketer> ObjCricketers = null;
  12. try
  13. {
  14. ObjCricketers = new CricketerDAL().GetCricketerList();
  15. }
  16. catch (Exception)
  17. {
  18. throw;
  19. }
  20. return ObjCricketers;
  21. }
  22. /// <summary>
  23. /// This method is used to get cricketers details by cricketer id
  24. /// </summary>
  25. /// <returns></returns>
  26. public List<Cricketer> GetCricketerDetailsById(int Id)
  27. {
  28. List<Cricketer> ObjCricketerDetails = null;
  29. try
  30. {
  31. ObjCricketerDetails = new CricketerDAL().GetCricketerDetailsById(Id);
  32. }
  33. catch (Exception)
  34. {
  35. throw;
  36. }
  37. return ObjCricketerDetails;
  38. }
  39. }
  40. }
Step 12: Build the solution.

Binding Model with View

In this section we will create the empty MVC 5 controller and add a view for displaying the details. We will also bind the view from the data by passing the data from the model to the controller. So, let’s begin with the following steps:

Step 1: In the Web Project, right click on the Controllers folder go to Add and click on the “New Scaffolded Item

Adding New Scaffolded Item
Figure 14: Adding New Scaffolded Item

Step 2: In the next wizard, select the “MVC 5 Empty Controller”.

Add Scaffold Wizard
Figure 15: Add Scaffold Wizard

Specify the controller name as “CricketersController

Adding New Controller
Figure 16: Adding New Controller

Step 3: Add a reference of Models and Core Project in the Web Project.

Step 4: In the CricketersController, replace the code with the following code:
  1. using CricketMasters.Core.BLL;
  2. using CricketMasters.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Web.Mvc;
  7. namespace CricketMastersWeb.Controllers
  8. {
  9. public class CricketersController : Controller
  10. {
  11. #region Variable
  12. /// <summary>
  13. /// This varaible is used for Cricketer BL
  14. /// </summary>
  15. public CricketerBL cricketerBL;
  16. #endregion
  17. #region Cricketer
  18. /// <summary>
  19. /// This method is used to get all cricketer names
  20. /// </summary>
  21. /// <returns></returns>
  22. [HttpGet, ActionName("GetAllCricketer")]
  23. public ActionResult GetAllCricketer()
  24. {
  25. List<Cricketer> objCricketer = new List<Cricketer>();
  26. var response = new CricketerBL().GetCricketerList();
  27. if (!object.Equals(response, null))
  28. {
  29. objCricketer = response.ToList();
  30. }
  31. return View("~/Views/Cricketers/Cricketer.cshtml", new Cricketer { Cricketers = objCricketer });
  32. }
  33. #endregion
  34. }
  35. }
In the above code, there is an action method which is used to get all cricketer names from the database. You can see that there is a highlighted code, that code is used to bind the model with the cricketer names which is further used to bind the cricketer names in the dropdownlist.

Step 5: Now add a view, by right click on the Views, then Cricketers

Adding View
Figure 17: Adding View

Step 6: Replace the view code with the following code:
  1. @model CricketMasters.Models.Cricketer
  2. @{
  3. ViewBag.Title = "Cricketer";
  4. }
  5. <h2>Cricketer Statistics</h2>
  6. <div class="row">
  7. <div class="col-md-8">
  8. <hr />
  9. @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  10. <div class="form-group">
  11. @Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label" })
  12. <div class="col-md-10">
  13. @Html.DropDownListFor(m => m.Name, new SelectList(Model.Cricketers, "ID", "Name"), new { @id = "playerNameList", @class = "form-control" })
  14. </div>
  15. </div>
  16. </div>
  17. </div>
Step 7: Go to the Web.Config file of the Web application and add the following connection string in the Connection Strings tab:
  1. <add name="CricketerConnectionString" connectionString="Data Source=Your Server Name;Initial Catalog=Cricketer;User ID=Your User ID;Password=Your Password" providerName="System.Data.SqlClient" />
Note: Please replace the highlighted code with your server credentials.

Step 8: Build the solution and now open Views, Shared, then _Layout.cshtml file and change the code with the highlighted code below:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>@ViewBag.Title - Cricket Masters Application</title>
  7. @Styles.Render("~/Content/css")
  8. @Scripts.Render("~/bundles/modernizr")
  9. </head>
  10. <body>
  11. <div class="navbar navbar-inverse navbar-fixed-top">
  12. <div class="container">
  13. <div class="navbar-header">
  14. <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
  15. <span class="icon-bar"></span>
  16. <span class="icon-bar"></span>
  17. <span class="icon-bar"></span>
  18. </button>
  19. @Html.ActionLink("Cricket Masters", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
  20. </div>
  21. <div class="navbar-collapse collapse">
  22. <ul class="nav navbar-nav">
  23. <li>@Html.ActionLink("Home", "Index", "Home")</li>
  24. <li>@Html.ActionLink("About", "About", "Home")</li>
  25. <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
  26. <li>@Html.ActionLink("Cricketers", "GetAllCricketer", "Cricketers")</li>
  27. </ul>
  28. @Html.Partial("_LoginPartial")
  29. </div>
  30. </div>
  31. </div>
  32. <div class="container body-content">
  33. @RenderBody()
  34. <hr />
  35. <footer>
  36. <p>© @DateTime.Now.Year - Cricket Masters</p>
  37. </footer>
  38. </div>
  39. @Scripts.Render("~/bundles/jquery")
  40. @Scripts.Render("~/bundles/bootstrap")
  41. @RenderSection("scripts", required: false)
  42. </body>
  43. </html>
Step 9: Now run the project. Click on the Cricketers link to open the view as in the following,

Opening View in MVC 5
Figure 18: Opening View in MVC 5

When you click on the “Cricketers” link, you can see that your DropDownList has the values which has been passed using the model. Take a look.

Binding Data in DropDownList Using Model
Figure 19: Binding Data in DropDownList Using Model

Binding Data with View using jQuery

In this section we will bind the details of the particular player in the table. We will use jQuery to send the id to the database to fetch the details of the player. So, let’s begin with the following steps:

Step 1: Add the following method in the “CricketersController”.
  1. /// <summary>
  2. /// This method is used to get all cricketer details based on the cricketer id
  3. /// </summary>
  4. /// <param name="CricketerId"></param>
  5. /// <returns></returns>
  6. [HttpGet, ActionName("GetCricketerDetailsById")]
  7. public JsonResult GetCricketerDetailsById(int CricketerId)
  8. {
  9. List<Cricketer> objCricketerDetails = new List<Cricketer>();
  10. var response = new CricketerBL().GetCricketerDetailsById(CricketerId);
  11. if (!object.Equals(response, null))
  12. {
  13. objCricketerDetails = response.ToList();
  14. }
  15. return Json(objCricketerDetails, JsonRequestBehavior.AllowGet);
  16. }
In the above method we get the details of the individual player and pass the data to the view in the JSON format.

Step 2: By adding the elements of table data your final view code is as in the following,
  1. @model CricketerApp.Model.Cricketer
  2. @{
  3. ViewBag.Title = "Cricketer";
  4. }
  5. <script src="~/Scripts/jquery-1.10.2.min.js"></script>
  6. <h2>Cricketer Statistics</h2>
  7. <div class="row">
  8. <div class="col-md-8">
  9. <hr />
  10. @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  11. <div class="form-group">
  12. @Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label" })
  13. <div class="col-md-10">
  14. @Html.DropDownListFor(m => m.Name, new SelectList(Model.Cricketers, "ID", "Name"), new { @id = "playerNameList", @class = "form-control", @Onchange = "return GetCricketerDetails();" })
  15. </div>
  16. </div>
  17. </div>
  18. </div><br />
  19. <div class="row">
  20. <div class="form-group">
  21. <div id="CricketerList" class="col-md-8"></div>
  22. </div>
  23. </div>
  24. <script type="text/javascript">
  25. $(document).ready(function () {
  26. GetCricketerDetails();
  27. });
  28. function GetCricketerDetails() {
  29. var cricketerId = $('#playerNameList option:selected').val();
  30. $.ajax({
  31. url: '@Url.Action("GetCricketerDetailsById", "Cricketers")',
  32. type: "GET",
  33. dataType: "json",
  34. data: { CricketerId: cricketerId },
  35. success: function (data) {
  36. $('#CricketerList').html(" ");
  37. var html = "";
  38. html += "<table class=\"table\">";
  39. html += "<tr>";
  40. html += "<th>";
  41. html += "@Html.DisplayNameFor(model=>model.Name)";
  42. html += "</th>";
  43. html += "<th>";
  44. html += "@Html.DisplayNameFor(model=>model.ODI)";
  45. html += "</th>";
  46. html += "<th>";
  47. html += "@Html.DisplayNameFor(model=>model.Tests)";
  48. html += "</th>";
  49. html += "<th>";
  50. html += "@Html.DisplayNameFor(model=>model.OdiRuns)";
  51. html += "</th>";
  52. html += "<th>";
  53. html += "@Html.DisplayNameFor(model=>model.TestRuns)";
  54. html += "</th>";
  55. html += "</tr>";
  56. $.each(data, function (index, item) {
  57. html += "<tr>";
  58. html += "<td>";
  59. html += "<lable>" + item.Name + "</lable>"
  60. html += "</td>";
  61. html += "<td>";
  62. html += "<lable>" + item.ODI + "</lable>"
  63. html += "</td>";
  64. html += "<td>";
  65. html += "<lable>" + item.Tests + "</lable>"
  66. html += "</td>";
  67. html += "<td>";
  68. html += "<lable>" + item.OdiRuns+ "</lable>"
  69. html += "</td>";
  70. html += "<td>";
  71. html += "<lable>" + item.TestRuns + "</lable>"
  72. html += "</td>";
  73. html += "</tr>";
  74. html += "</table>";
  75. });
  76. $('#CricketerList').append(html);
  77. }
  78. });
  79. return false;
  80. }
  81. </script>
Step 3: Save the page and run the project. You can see in the following screenshot that the data is bind to the table.

Binding Data in View using jQuery
Figure 20: Binding Data in View using jQuery

You can now change the selection and you will get the output.

Binding Data in View in MVC 5
Figure 21: Binding Data in View in MVC 5

That’s it.

Summary

This article described how to bind the data to the view by passing the model in the controller. We also learned to bind the data using the jQuery to the table in the View. Thanks for reading the article.