This article explains the following,

Design your Database
In this article I used the following table and stored procedure.

table

  1. --Use the below script to create the table
  2. CREATE TABLE Mas_Employee
  3. (
  4. [Id] [int] PRIMARY KEY IDENTITY(1,1) NOT NULL,
  5. [Name] [varchar](50) NULL,
  6. [Gender] [varchar](50) NULL,
  7. [Salary] [int] NULL,
  8. [DeptId] [int] NULL
  9. )
  10. --Procedure to select all the employees
  11. Create procedure USP_GetAllEmployees 1
  12. @DeptId Int = NUll
  13. as
  14. Begin
  15. Select E.Id, E.Name, E.Gender, E.Salary, E.DeptId
  16. From Mas_Employee E
  17. Where E.DeptId = ISNULL(@DeptId, DeptId)
  18. End
Step 1: Create New Project:

Create New Project

Step 2:
Connection string in Web.Config.

Step 3: Create Business Entities Layer (BE).

Step 4: Create Data Access Layer (DL).

Step 5: Add references.

Step 6: Add a Controller.

Right click on controller folder, click on Add -> Controller and name it EmployeeController. Then click on Add.

Add controller

Copy and paste the following code in your EmployeeController.

  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Web.Mvc;
  4. using MVC_DataAccessLayer;
  5. using MVC_BusinessEntities;
  6. namespace MVCDemo.Controllers
  7. {
  8. public class EmployeeController: Controller
  9. {
  10. public ActionResult Index()
  11. {
  12. DL_Employee dal = new DL_Employee();
  13. List < BE_Employee > employees = dal.Employees.ToList();
  14. return View(employees);
  15. }
  16. }
  17. }
Step 7: Add View
  • Right click on the Index() action method in the "EmployeeController" class.
  • Then select "Add View" from the context menu.
  • Next select View name = Index, View engine = Razor
  • Select "Create a strongly-typed view" checkbox
  • Scaffold Template = List
  • Click "Add" button
  • Then automatically it will generate the default code in your view.
Model:
It retrieve the application data from Database and it also contain business logic to change the state mention by controller. But in this article by observing all the above steps, we came to know that we are not using Model folder for any instance.
Step 8: Build your Solution and press F5 to run this program. The output is as follows:
Index
I hope you enjoyed this article. Please provide your valuable suggestions and feedback.