Open your favorite SQL server database -- any version. It doesn’t really matter. Create customer table.
Step 2
Now open your favorite visual studio 2017 or any version of your choice.
Step 3
Create an empty project in visual studio and give an appropriate name. Checked MVC checkbox and click on OK.
Step 4
Right-click on the Models folder and add a database model. Add Entity Framework now. For that, right-click on Models folder, select Add, then select New Item.
You will get a window; from there, select Data from the left panel and choose ADO.NET Entity Data Model, give it the name MyModel (this name is not mandatory, you can give any name) and click "Add".
After you click on "Add a window", the wizard will open. Choose EF Designer from the database and click "Next".
After clicking on "Next", a window will appear. Choose New Connection. Another window will appear. Add your server name - if it is local, then enter a dot (.). Choose your database and click "OK".
The connection will be added. If you wish, save the connection name as you want. You can change the name of your connection below. It will save the connection in the web config. Now, click "Next".
After clicking on NEXT, another window will appear. Choose the database table name as shown in the below screenshot and click "Finish".
Entity Framework gets added and the respective class gets generated under the Models folder.
Step 5
Right-click on Controllers folder add a controller.
A window will appear. Choose MVC5 Controller-Empty and click "Add".
After clicking on "Add", another window will appear with DefaultController. Change the name to HomeController and click "Add". The HomeController will be added under the Controllers folder. Don’t change the Controller suffix for all controllers, change only the highlight, and instead of Default, just change Home.
Controller Class Code
- using MvcAlphabeticSearch_Demo.Models;
- using System.Linq;
- using System.Web.Mvc;
-
- namespace MvcAlphabeticSearch_Demo.Controllers
- {
- public class HomeController : Controller
- {
- private readonly CustomerContext db = new CustomerContext();
-
- public ActionResult Index()
- {
- return View();
- }
-
- public JsonResult GetCustomer()
- {
- var customer = db.Customers.ToList();
- return Json(new { data = customer }, JsonRequestBehavior.AllowGet);
- }
- }
- }
Step 6
Right click on Index action method in controller class to “Add View”
- @{
- ViewBag.Title = "Index";
- }
-
- <link href="@Url.Content("~/css/dataTables.alphabetSearch.css")" rel="stylesheet" />
- <link href="@Url.Content("~/Content/DataTables/css/dataTables.bootstrap4.min.css")" rel="stylesheet" />
- <table id="example" class="display table table-bordered">
- <thead class="thead-dark text-uppercase text-white">
- <tr>
- <th>First Name</th>
- <th>Last Name</th>
- <th>City</th>
- <th>Country</th>
- <th>Phone Number</th>
- </tr>
- </thead>
- <tbody></tbody>
- </table>
- <script src="@Url.Content("~/Scripts/jquery-3.4.1.min.js")" type="text/javascript"></script>
- <script src="@Url.Content("~/Scripts/bootstrap.min.js")" type="text/javascript"></script>
- <script src="@Url.Content("~/Scripts/DataTables/jquery.dataTables.min.js")" type="text/javascript"></script>
- <script src="@Url.Content("~/Scripts/DataTables/dataTables.bootstrap4.min.js")" type="text/javascript"></script>
- <script src="@Url.Content("~/js/dataTables.alphabetSearch.min.js")" type="text/javascript"></script>
- <script>
- $(document).ready(function () {
- var table = $('#example').DataTable({
- dom: 'Alfrtip',
- alphabetSearch: {
- column: 0
- },
- "ajax": {
- "url": "/Home/GetCustomer",
- "type": "GET",
- "datatype": "json"
- },
- "columns": [
- { "data": "FirstName" },
- { "data": "LastName" },
- { "data": "City" },
- { "data": "Country" },
- { "data": "Phone" }
- ]
- });
- });
- </script>
Step 7
Install the latest version of bootstrap, jquery and JQuery Datatable from NuGet package manager under tools in visual studio.
Step 8
Build your project and Run by pressing Ctrl+F5