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