ASP .NET core 基础入门详解

部门接口定义

    public interface IDepartmentService
    {
        //获取所有部门
        Task<IEnumerable<Department>> GetALL();
        //根据ID获取部门
        Task<Department> GetById(int id);
        //公司整体情况
        Task<ComparySummary> GetComparySummary();
        //添加部门
        Task Add(Department department);
    }

部门接口实现

 public class DepartmentService : IDepartmentService
    {
        private readonly List<Department> _departments = new List<Department>();

        public DepartmentService()
        {
            _departments.Add(new Department
            {
                Id = 1,
                Name = "HR",
                EmploteeCount = 16,
                Location = "beijing"
            });
            _departments.Add(new Department
            {
                Id = 1,
                Name = "HR",
                EmploteeCount = 16,
                Location = "beijing"
            });
            _departments.Add(new Department
            {
                Id = 2,
                Name = "RD",
                EmploteeCount = 52,
                Location = "shanghai"
            });
            _departments.Add(new Department
            {
                Id = 3,
                Name = "Sales",
                EmploteeCount = 200,
                Location = "China"
            });
        }

        public Task<IEnumerable<Department>> GetALL()
        {
            return Task.Run(function: () => _departments.AsEnumerable());
        }

        public Task<Department> GetById(int id)
        {
            return Task.Run(function: () => _departments.FirstOrDefault(x => x.Id == id));
        }
        public Task<ComparySummary> GetComparySummary()
        {
            return Task.Run(function: () =>
            {
                return new ComparySummary
                {
                    EmployeeCount = _departments.Sum(x => x.EmploteeCount),
                    AverageDepartmentEmployeeCount = (int)_departments.Average(x => x.EmploteeCount)
                };
            });
        }
        public Task Add(Department department)
        {
            department.Id = _departments.Max(x => x.Id) + 1;
            _departments.Add(department);
            return Task.CompletedTask;

        }
    }

员工接口定义

 public interface IEmployeeService
    {
        //添加员工
        Task Add(Employee employee);
        //根据部门ID 查找 该部门的所有员工
        Task<IEnumerable<Employee>> GetByDepartmentId(int departmentId);
        //开除员工
        Task<Employee> Fire(int id);
    }

员工接口实现

public class EmploteeService : IEmployeeService
    {
        private readonly List<Employee> _employees = new List<Employee>();
        public EmploteeService()
        {
            _employees.Add(new Employee
            {
                Id = 1,
                DepartmentId = 1,
                FirstName = "Nick",
                LastName = "Carter",
                Gender = Gender.});
            _employees.Add(new Employee
            {
                Id = 2,
                DepartmentId = 1,
                FirstName = "Michael",
                LastName = "Carter",
                Gender = Gender.});
            _employees.Add(new Employee
            {
                Id = 3,
                DepartmentId = 1,
                FirstName = "Mariah",
                LastName = "Carter",
                Gender = Gender.});
            _employees.Add(new Employee
            {
                Id = 4,
                DepartmentId = 2,
                FirstName = "Axl",
                LastName = "Rose",
                Gender = Gender.});
            _employees.Add(new Employee
            {
                Id = 3,
                DepartmentId = 1,
                FirstName = "Mariah",
                LastName = "Carter",
                Gender = Gender.});
            _employees.Add(new Employee
            {
                Id = 5,
                DepartmentId = 2,
                FirstName = "Kate",
                LastName = "Winlet",
                Gender = Gender.});
            _employees.Add(new Employee
            {
                Id = 6,
                DepartmentId = 3,
                FirstName = "Rob",
                LastName = "Thomas",
                Gender = Gender.});
            _employees.Add(new Employee
            {
                Id = 7,
                DepartmentId = 3,
                FirstName = "Avril",
                LastName = "lacione",
                Gender = Gender.});
            _employees.Add(new Employee
            {
                Id = 8,
                DepartmentId = 3,
                FirstName = "Kety",
                LastName = "perry",
                Gender = Gender.});
            _employees.Add(new Employee
            {
                Id = 9,
                DepartmentId = 3,
                FirstName = "Michelle",
                LastName = "Monaghao",
                Gender = Gender.});
        }

        public Task Add(Employee employee)
        {
            employee.Id = _employees.Max(X => X.Id) + 1;
            _employees.Add(employee);
            return Task.CompletedTask;
        }
        public Task<IEnumerable<Employee>> GetByDepartmentId(int departmentId)
        {
            return Task.Run(function: () => _employees.Where(x => x.DepartmentId == departmentId));
        }
        public Task<Employee> Fire(int id)
        {
            return Task.Run(function: () =>
            {
                var employee = _employees.FirstOrDefault(x => x.Id == id);
                if (employee != null)
                {
                    employee.Fired = true;
                    return employee;
                }
                return null;
            });

        }
    }

部门控制器

public class HomeController : Controller
    {
        private readonly IDepartmentService _departmentService;
        public HomeController(IDepartmentService departmentService)
        {
            _departmentService = departmentService;
        }
        public async Task<IActionResult> index()
        {
            ViewBag.Title = "Department Index";
            var departments = await _departmentService.GetALL();
            return View(departments);
        }
        [HttpGet]
        public IActionResult Add()
        {
            ViewBag.Title = "Add Deparment";
            return View(new Department());

        }
        [HttpPost]
        public async Task<IActionResult> Add(Department department)
        {
            //判断是否合法
            if (ModelState.IsValid)
            {
                await _departmentService.Add(department);
            }
            return RedirectToAction(nameof(index));
        }
    }

部门Index视图

@using Net_Core.新文件夹
@model IEnumerable<Department>

<div class="row">
    <div class="col-md-10 offset-md-2">
        <table class="table table-hover table-bordered">
            <tr>
                <th>编号</th>
                <th>名称</th>
                <th>地址</th>
                <th>该部门下员工数</th>
                <th>操作</th>
            </tr>
            <tbody>
                @foreach (var item in Model)
                {
                <tr>
                    <td>@item.Id</td>
                    <td>@item.Name</td>
                    <td>@item.Location</td>
                    <td>@item.EmploteeCount</td>

                    <td>
                        <a asp-controller="Employee" asp-action="Index" asp-route-departmentId="@item.Id">该部门的所有员工</a>
                       
                    </td>
                </tr>
            }
                </tbody>
            @*@Html.DisplayForModel();*@
            </table>
    </div>
</div>

<div class="row">
    <div class="col-md-4 offset-md-2">
        <a href="/home/add">Add</a>
    </div>
</div>

部门Add视图

@model Net_Core.新文件夹.Department

    <form asp-action="add">
        <div class="row form-group">
            <div class="col-md-2 offset-md-2">
                <label asp-for="Name"></label>
            </div>
            <div class="col-md-6">
                <input class="form-control" asp-for="Name" />
            </div>
        </div>
        <div class="row form-group">
            <div class="col-md-2 offset-md-2">
                <label asp-for="Location"></label>
            </div>
            <div class="col-md-6">
                <input class="form-control" asp-for="Location" />
            </div>
        </div>
        <div class="row form-group">
            <div class="col-md-2 offset-md-2">
                <label asp-for="EmploteeCount"></label>
            </div>
            <div class="col-md-6">
                <input class="form-control" asp-for="EmploteeCount" />
            </div>
        </div>
        <div class="row form-group">
            <div class="col-md-2 offset-md-2">
                <input value="提交" type="submit" />
            </div>
            
        </div>
    </form>

员工控制器

  public class EmployeeController : Controller
    {
        private readonly IDepartmentService _departmentService;
        private readonly IEmployeeService _employeeService;

        public EmployeeController(IDepartmentService departmentService, IEmployeeService employeeService)
        {
            _departmentService = departmentService;
            _employeeService = employeeService;
        }
        public async Task<IActionResult> index(int departmentId)
        {
            var department = await _departmentService.GetById(departmentId);

            ViewBag.Title = $"Employees of {department.Name}";
            ViewBag.DepartmentId = departmentId;
            var emplotess = await _employeeService.GetByDepartmentId(departmentId);
            return View(emplotess);
        }

        [HttpGet]
        public IActionResult Add(int departmentId)
        {
            ViewBag.Title = "Add Employee";
            return View(new Employee
            {
                DepartmentId = departmentId
            });
        }

        [HttpPost]
        public async Task<IActionResult> Add(Employee employee)
        {
            if (ModelState.IsValid)
            {
                await _employeeService.Add(employee);
            }
            return RedirectToAction(nameof(index), routeValues: new { departmentId = employee.DepartmentId });
        }
        //开除员工
        public async Task<IActionResult> Fire(int employeeId)
        {
            var employee = await _employeeService.Fire(employeeId);
            
            return RedirectToAction(nameof(index), routeValues: new { departmentId = employee.DepartmentId });

        }




    }

员工 Index


@{
    ViewData["Title"] = "index";
}
@model IEnumerable<Net_Core.新文件夹.Employee>
<div class="row">
    <div class="col-md-10 offset-md-2">
        <table class="table table-hover table-bordered">
            <tr>
                <th>FirstName</th>
                <th>LastName</th>
                <th>Gender</th>
                <th>操作</th>
            </tr>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>@item.FirstName</td>
                        <td>@item.LastName</td>
                        <td> @item.Gender</td>
                        <td>
                            @(item.Fired ? "是" : "")

                        </td>

                        <td>
                            @if (!item.Fired)
                            {
                                <a asp-controller="Employee" asp-action="Fire" asp-route-employeeId="@item.Id">Fire</a>
                            }
                        </td>


                    </tr>
                }
            </tbody>
            @*@Html.DisplayForModel();*@
        </table>
        <a asp-controller="Employee" asp-action="Add" asp-route-departmentId="@ViewBag.DepartmentId">Add</a>
    </div>
</div>

员工新增 Add视图


@{
    ViewData["Title"] = "Add";
}
@using Net_Core.新文件夹
@model Net_Core.新文件夹.Employee
<form asp-action="add">
    <input type="hidden" asp-for="DepartmentId" />
    <div class="row form-group">
        <div class="col-md-2 offset-md-2">
            <label asp-for="FirstName"></label>
        </div>
        <div class="col-md-6">
            <input class="form-control" asp-for="FirstName" />
        </div>
    </div>
    <div class="row form-group">
        <div class="col-md-2 offset-md-2">
            <label asp-for="LastName"></label>
        </div>
        <div class="col-md-6">
            <input class="form-control" asp-for="LastName" />
        </div>
    </div>
    <div class="row form-group">
        <div class="col-md-2 offset-md-2">
            <label asp-for="Gender"></label>
        </div>
        <div class="col-md-6">
            @*<input class="form-control" asp-for="Gender" />*@
            <select class="form-control" asp-for="Gender" asp-items="Html.GetEnumSelectList()">
                
            </select>

        </div>
    </div>
    <div class="row form-group">
        <div class="col-md-2 offset-md-2">
            <input value="提交" type="submit" />
        </div>

    </div>
</form>


你可能感兴趣的:(.Net,Core,asp.net)