In this and in few upcoming Videos, I am going to discuss Business Objects as Model in ASP.NET MVC application. Please read our previous three Videos where we discussed how to use Entity Framework and use entities as our models.

The entities are mapped to the database tables and object-relational mapping (ORM) frameworks like Entity Framework, NHibernate, etc. are used to retrieve and save the data into a database. The business object contains both state (data) and behavior that is logic specific to the business. Let us understand how to use Business Objects as Model in ASP.NET MVC Application.
Create an Empty ASP.NET MVC Application:
First, create an Empty MVC Application with the name MVC_DEMO. Once you create the application, then create a controller with the name as HomeController within the Controllers Folder and then copy and paste the below code in it.
The following URL will invoke the Index() action method of the HomeController. Notice that, the HomeController class is inherited from the base Controller class which in turn inherits from ControllerBase class. ControllerBase, in turn, inherits from the IController interface.
http://localhost:53657/Home/Index
The return View() statement within the index action method by default looks for a view with the name “Index” in “/Views/Home/” and “/Views/Shared/” folders. If a view with the name “Index” is not found then we will get the following error.
So, In the ASP.NET MVC application, there are several conventions that we need to follow while working. For example, controllers need to have the word controller in them and should implement the IController interface either directly or indirectly. Views should be placed in a specific location that MVC can find them.
But with models, there are no strict rules. In fact, the “Models” folder is optional and they can place anywhere within the application. They can even be present in a separate project. Lets now turn our attention to using business objects as the model. We will be using the table “Employee” for this demo.
Step1: Create the Required Database
Please use the below SQL script to create and populate the Employee table with some test data. Also, we are creating one stored procedure to retrieve the employee data.
Step2: Add a Class Library project with Name=”BusinessLayer” to the Solution
Right-click on the Solution Folder => Add => New Project as shown in the below image.
From the new project window, select Visual C# from Installed Template from the left pane and then select Class Library Template from the middle pane. Provide the name as BusinessLayer and click on the OK as shown in the below image.
Now it will add the BusinessLayer class library project to our existing solution.
Step3: Adding Models to the Class Library Project
Right-click on the business layer class library project and add a class file with the name Employee.cs. Once you created the Employee class then copy and paste the following code into it. The following class is very straightforward. We simply created the class with 6 properties.
Step4: Adding Required References
Right-click on the “References” folder of the business layer class library project and add a reference to the “System.Configuration” assembly. This is required as we want to read the connection string from the web config file using the ConfigurationManager class and this class belongs to System.Configuration namespace.
Step5: Adding EmployeeBusinessLayer class
Right-click on the business layer class library project and add a class file with the name EmployeeBusinessLayer.cs. Once you created the EmployeeBusinessLayer class then copy and paste the following code into it. In the following class, we define one method i..e. GetAllEmployess(). This method is used to get the employee details from the database. The following code is self-explained, so please go through the comment lines.
Step6: Adding a Reference to class Library Project in ASP.NET MVC Application:
Right-click on the “References” folder of the “MVC_DEMO” project and add a reference to the “BusinessLayer” class library project. Then Include a connection string with name = “DBCS” in Web.Config file as shown below.
Step8: Creating Controller
Right-click on the “Controllers” folder and add a Controller with the name “EmployeeController” and then copy and paste the following code into it. In the below controller we have only one action method i.e. Index. This method creates an instance of EmoloyeeBusinessLayer class and then calls the GetAllEmployees method which will return the list of employees. The list of employees is then handed over to the Index view.
Step9: Adding Index View
Right-click on the Index() action method in the “EmployeeController” class and then select “Add View” from the context menu. Set
View name = Index
Model class = Employee (BusinessLayer)
Template = List
Click on the “Add” button as shown below
Change the RouteConfig.cs as shown below
We are setting the controller as Employee and the default action method as Index.
Set MVC_DEMO as your startup project and run the application, then navigate to http://localhost:54094/Employee/Index. It should display the output as expected as shown in the below image.