0% found this document useful (0 votes)
25 views

Using Business Objects As Model in MVC

The document provides steps to create an MVC application that uses a business layer and data access layer. It involves: 1) Creating an MVC application and class library for the business layer. 2) Defining an Employee class and business logic class to retrieve employee data. 3) Adding a database connection string and stored procedure to retrieve employees. 4) Creating an Employee controller and view to display the employee list using the business logic class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Using Business Objects As Model in MVC

The document provides steps to create an MVC application that uses a business layer and data access layer. It involves: 1) Creating an MVC application and class library for the business layer. 2) Defining an Employee class and business logic class to retrieve employee data. 3) Adding a database connection string and stored procedure to retrieve employees. 4) Creating an Employee controller and view to display the employee list using the business logic class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Using business objects as model in mvc

Suggested Videos

create one controller name as home


public class HomeController : Controller
{
public ViewResult Index()
{
ViewData["Countries"] = new List<string>()
{
"India",
"US",
"UK",
"Canada"
};

return View();
}
}

Create table Employee


(
Id int Primary Key Identity(1,1),
Name nvarchar(50),
Gender nvarchar(10),
City nvarchar(50),
DateOfBirth DateTime
)

insert 4 record

Stored procedure to retrieve data


Create procedure spGetAllEmployees
as
Begin
Select Id, Name, Gender, City, DateOfBirth
from tblEmployee
End

Step 1: Create an ASP.NET MVC 4 Web application with name = MVCDemo

Step 2: Add a Class Library project with Name="BusinessLayer"

Step 3: Right click on the BusinessLayer class library project, and add a class file
with name = Employee.cs.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusinessLayer
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public DateTime DateOfBirth { get; set; }
}
}

Step 4: Right click on the "References" folder of the "BusinessLayer" class library
project, and add a reference to "System.Configuration" assembly.

Step 5: Right click on the BusinessLayer class library project, and add a class file
with name = EmployeeBusinessLayer.cs.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace BusinessLayer
{
public class EmployeeBusinessLayer
{
public IEnumerable<Employee> Employees
{
get
{
string connectionString =
ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

List<Employee> employees = new List<Employee>();

using (SqlConnection con = new SqlConnection(connectionString))


{
SqlCommand cmd = new SqlCommand("spGetAllEmployees", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Employee employee = new Employee();
employee.ID = Convert.ToInt32(rdr["Id"]);
employee.Name = rdr["Name"].ToString();
employee.Gender = rdr["Gender"].ToString();
employee.City = rdr["City"].ToString();
employee.DateOfBirth = Convert.ToDateTime(rdr["DateOfBirth"]);

employees.Add(employee);
}
}

return employees;
}
}
}
}

Step 6: Right click on the "References" folder of the "MVCDemo" project, and add a
reference to "BusinessLayer" project.

Step 7: Include a connection string with name = "DBCS" in Web.Config file


<add name="DBCS"
connectionString="server=.; database=Sample; integrated security=SSPI"
providerName="System.Data.SqlClient"/>

Step 8: Right click on the "Controllers" folder and add Controller with name
= "EmployeeController.cs".
public class EmployeeController : Controller
{
public ActionResult Index()
{
EmployeeBusinessLayer employeeBusinessLayer =
new EmployeeBusinessLayer();

List<Employee> employees = employeeBusinessLayer.Employees.ToList();


return View(employees);
}
}

Step 9: Right click on the Index() action method in the "EmployeeController" class
and select "Add View" from the context menu. Set
View name = Index
View engine = Razor
Select "Create a strongly-typed view" checkbox
Scaffold Template = List
Click "Add" button

You might also like