Created
December 9, 2015 11:47
-
-
Save Tesla9527/6c85c738e958b05db355 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
----------------------------- | |
Below is code in Employee.cs in project BusinessLayer | |
----------------------------- | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
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 Birthday { get; set; } | |
} | |
} | |
----------------------------- | |
Below is code in EmployeeBusinessLayer.cs in project BusinessLayer | |
----------------------------- | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Configuration; | |
using System.Data; | |
using System.Data.SqlClient; | |
namespace BusinessLayer | |
{ | |
public class EmployeeBusinessLayer | |
{ | |
public IEnumerable<Employee> Employees | |
{ | |
get | |
{ | |
string connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString; | |
List<Employee> employees = new List<Employee>(); | |
using (SqlConnection con = new SqlConnection(connectionString)) | |
{ | |
SqlCommand cmd = new SqlCommand("spGetAllEmployee",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.Birthday = Convert.ToDateTime(rdr["Birthday"]); | |
employees.Add(employee); | |
} | |
} | |
return employees; | |
} | |
} | |
} | |
} | |
----------------------------- | |
Below is code in EmployeeController.cs in project MVCDemo | |
----------------------------- | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
using BusinessLayer; | |
namespace MVCDemo.Controllers | |
{ | |
public class EmployeeController : Controller | |
{ | |
// GET: Employee | |
public ActionResult Index() | |
{ | |
EmployeeBusinessLayer employeeBusinessLayer = new EmployeeBusinessLayer(); | |
List<Employee> employees = employeeBusinessLayer.Employees.ToList(); | |
return View(employees); | |
} | |
} | |
} | |
----------------------------- | |
Below is code in EmployeeController.cs in project MVCDemo | |
----------------------------- | |
@model IEnumerable<BusinessLayer.Employee> | |
@{ | |
ViewBag.Title = "Index"; | |
} | |
<div style="font-family:Arial"> | |
<h2>Index</h2> | |
<p> | |
@Html.ActionLink("Create New", "Create") | |
</p> | |
<table class="table"> | |
<tr> | |
<th> | |
@Html.DisplayNameFor(model => model.Name) | |
</th> | |
<th> | |
@Html.DisplayNameFor(model => model.Gender) | |
</th> | |
<th> | |
@Html.DisplayNameFor(model => model.City) | |
</th> | |
<th> | |
@Html.DisplayNameFor(model => model.Birthday) | |
</th> | |
<th></th> | |
</tr> | |
@foreach (var item in Model) | |
{ | |
<tr> | |
<td> | |
@Html.DisplayFor(modelItem => item.Name) | |
</td> | |
<td> | |
@Html.DisplayFor(modelItem => item.Gender) | |
</td> | |
<td> | |
@Html.DisplayFor(modelItem => item.City) | |
</td> | |
<td> | |
@Html.DisplayFor(modelItem => item.Birthday) | |
</td> | |
<td> | |
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) | | |
@Html.ActionLink("Details", "Details", new { id = item.ID }) | | |
@Html.ActionLink("Delete", "Delete", new { id = item.ID }) | |
</td> | |
</tr> | |
} | |
</table> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment