routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DataLayer
{
public class Employee
{
public int EmpID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string Address { get; set; }
public double Salary { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace DataLayer
{
public class EmployeeBusinessLayer
{
public IEnumerable<Employee> employees
{
get
{
string conString = ConfigurationManager.ConnectionStrings["EmpContext"].ConnectionString;
List<Employee> empList = new List<Employee>();
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand("select * from tblemps", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Employee empObj = new Employee();
//Initialization of Employee cbject
empObj.EmpID = Convert.ToInt32(dr["EmpId"]);
empObj.Name = dr["Name"].ToString();
empObj.Gender = dr["Gender"].ToString();
empObj.Salary = Convert.ToDouble(dr["Salary"]);
empObj.Address = dr["Address"].ToString();
empList.Add(empObj);
}
}
return empList;
}
}
}
}
<connectionStrings>
<add name="EmpContext" connectionString ="Integrated Security=true;Initial Catalog=Employee;Data Source=.;" providerName="System.Data.SqlClient"/>
</connectionStrings>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataLayer;
namespace MvcBusinessObject.Controllers
{
public class EmployeeController : Controller
{
public ActionResult Index()
{
EmployeeBusinessLayer empBusLayer = new EmployeeBusinessLayer();
List<Employee> empList= empBusLayer.employees.ToList();
return View(empList);
}
}
}
@model IEnumerable<DataLayer.Employee>
@{
ViewBag.Title = "Index";
}
<h2>Employee Details</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<style>
table, th, td
{
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd)
{
background-color: #ffe6e6;
}
table tr:nth-child(even)
{
background-color: #ccffcc;
}
</style>
<table style="font-family:Arial; font-size:medium; color:darkblue">
<tr>
<th>
@Html.DisplayNameFor(model => model.EmpID)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Gender)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th>
@Html.DisplayNameFor(model => model.Salary)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.EmpID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Gender)
</td>
<td>
@Html.DisplayFor(modelItem => item.Address)
</td>
<td>
@Html.DisplayFor(modelItem => item.Salary)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ })|
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })|
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Employee", action = "Index", id = UrlParameter.Optional }
);