MvcAppEmployeesFromDb.DataAccessLayer

来源:互联网 发布:恺英网络招聘 编辑:程序博客网 时间:2024/05/17 18:00

insert into TblEmployee values('johnson','fernandes',14000);
insert into TblEmployee values('michael','jackson',16000);
insert into TblEmployee values('robert','pattinson',20000);


  <connectionStrings>
    <add connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SalesERPDB;Integrated Security=True"
            name="SalesERPDAL"
            providerName="System.Data.SqlClient"/>
  </connectionStrings>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using MvcAppEmployeesFromDb.Models;

namespace MvcAppEmployeesFromDb.DataAccessLayer
{
    public class SalesERPDAL : DbContext
    {
        public DbSet<Employee> Employees { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Employee>().ToTable("TblEmployee");
            base.OnModelCreating(modelBuilder);
        }

    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcAppEmployeesFromDb.DataAccessLayer;

namespace MvcAppEmployeesFromDb.Models
{
    public class EmployeeListViewModel
    {
        public List<Employee> Employees
        {
            get
            {
                SalesERPDAL salesDal = new SalesERPDAL();
                return salesDal.Employees.ToList();
            }
        }

        public string UserName { get; set; }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace MvcAppEmployeesFromDb.Models
{
    public class Employee
    {
        [Key]
        public int EmployeeId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Salary { get; set; }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAppEmployeesFromDb.Models;

namespace MvcAppEmployeesFromDb.Controllers
{
    public class TestController : Controller
    {
        //
        // GET: /Test/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetEmployeeListView()
        {
            EmployeeListViewModel employeeListViewModel = new EmployeeListViewModel();
            employeeListViewModel.UserName = "Sky";
            return View("EmployeeListView", employeeListViewModel);
        }
    }
}


@using MvcAppEmployeesFromDb.Models
@model EmployeeListViewModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>EmployeeListView</title>
</head>
<body>
     Hello @Model.UserName
    <hr />
    <div>
    <table>
    <tr>
    <th>Employee Name</th>
    <th>Salary</th>
    </tr>
         @foreach (Employee item in Model.Employees)
         {
    <tr>
    <td>@item.FirstName @item.LastName</td>
            @if(item.Salary>15000)
            {
            <td style="background-color:yellow">
                    Employee Salary: @item.Salary.ToString("C")
            </td>
            }
            else
            {          
            <td style="background-color:green">
      
                    Employee Salary: @item.Salary.ToString("C")
            </td>
            }
    </tr>
         }
    </table>
    </div>
</body>
</html>


0 0
原创粉丝点击