1.静态文件Bootstrap
2.Models 添加 三个类
namespace Three.Models
{
public class Employee
{
public int Id { get; set; }
public int DepartedmentId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Gender Gender { get; set; }
public bool Fired { get; set; }
}
public enum Gender
{
女 = 0,
男 = 1,
其他 = 2
}
}
namespace Three.Models
{
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
public int Employee { get; set; }
}
}
namespace Three.Models
{
public class CompanySummary
{
public int EmployeeCount { get; set; }
public int AverageDepartmentEmployeeCount { get; set; }
}
}
3.Services 添加2套服务
第一套
amespace Three.Services
{
public interface IDepartmentService//接口
{
Task<IEnumerable<Department>> GetAll();
Task<Department> GetById(int id);
Task<CompanySummary> GetCompanySummary();
Task Add(Department department);
}
}
※Task.Run(function:() => _departments.AsEnumerable());※
※ return Task.CompletedTask;※
namespace Three.Services
{
public class DepartmentService:IDepartmentService//实现
{
private readonly List<Department> _departments = new List<Department>();
public DepartmentService()
{
_departments.Add(new Department
{
Id = 1,
Name = "HR",
EmployeeCount = 16,
Location = "Beijing"
});
_departments.Add(new Department
{
Id = 2,
Name = "R&D",
EmployeeCount = 52,
Location = "Shanghai"
});
_departments.Add(new Department
{
Id = 3,
Name = "Sales",
EmployeeCount = 200,
Location = "China"
});
}
public Task Add(Department department)
{
department.Id = _departments.Max(x => x.Id) + 1;
_departments.Add(department);
return Task.CompletedTask;
}
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<CompanySummary> GetCompanySummary()
{
return Task.Run(function:() =>
{
return new CompanySummary
{
EmployeeCount = _departments.Sum(x => x.EmployeeCount),
AverageDepartmentEmployeeCount = (int)_departments.Average(x => x.EmployeeCount)
};
});
}
}
}
第二套
namespace Three.Services
{
public interface IEmployeeService
{
Task Add(Employee employee);
Task<IEnumerable<Employee>> GetDepartmentId(int departmentId);
Task<Employee> Fire(int id);
}
}
namespace Three.Services
{
public class EmployeeService : IEmployeeService
{
private readonly List<Employee> _employees = new List<Employee>();
public EmployeeService()
{
_employees.Add(new Employee
{
Id=1,
DepartedmentId =1,
FirstName = "Nick",
LastName = "Carter",
Gender = Gender.男
});
_employees.Add(new Employee
{
Id = 2,
DepartedmentId = 1,
FirstName = "Nick",
LastName = "Carter",
Gender = Gender.男
});
_employees.Add(new Employee
{
Id = 3,
DepartedmentId = 1,
FirstName = "Nick",
LastName = "Carter",
Gender = Gender.男
});
_employees.Add(new Employee
{
Id = 4,
DepartedmentId = 1,
FirstName = "Nick",
LastName = "Carter",
Gender = Gender.男
});
_employees.Add(new Employee
{
Id = 5,
DepartedmentId = 1,
FirstName = "Nick",
LastName = "Carter",
Gender = Gender.男
});
_employees.Add(new Employee
{
Id = 6,
DepartedmentId = 3,
FirstName = "Nick",
LastName = "Carter",
Gender = Gender.男
});
_employees.Add(new Employee
{
Id = 7,
DepartedmentId = 1,
FirstName = "Nick",
LastName = "Carter",
Gender = Gender.男
});
_employees.Add(new Employee
{
Id = 8,
DepartedmentId = 2,
FirstName = "Nick",
LastName = "Carter",
Gender = Gender.男
});
_employees.Add(new Employee
{
Id = 9,
DepartedmentId = 3,
FirstName = "Nick",
LastName = "Carter",
Gender = Gender.女
});
}
public Task Add(Employee employee)
{
employee.Id = _employees.Max(x => x.Id) + 1;
_employees.Add(employee);
return Task.CompletedTask;
}
public Task<Employee> Fire(int id)
{
return Task.Run(function: () =>
{
var employee = _employees.FirstOrDefault(e => e.Id == id);
if(employee != null)
{
employee.Fired = true;
return employee;
}
return null;
});
}
public Task<IEnumerable<Employee>> GetDepartmentId(int departmentId)
{
return Task.Run(function: () => _employees.Where(x => x.DepartedmentId == departmentId));
}
}
}
4.startup类注册服务
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddSingleton<IDepartmentService, DepartmentService>();
services.AddSingleton<IEmployeeService, EmployeeService>();
}
AddSingleton→AddTransient→AddScoped
AddSingleton的生命周期:
项目启动-项目关闭 相当于静态类 只会有一个
AddScoped的生命周期:
请求开始-请求结束 在这次请求中获取的对象都是同一个
AddTransient的生命周期:
请求获取-(GC回收-主动释放) 每一次获取的对象都不是同一个
5.添加两个控制器
namespace Three.Controllers
{
public class DepartmentController : Controller
{
private readonly IDepartmentService _departmentService;
public DepartmentController(IDepartmentService departmentService)
{
_departmentService = departmentService;
}
public async Task<IActionResult> Index()
{
ViewBag.Title = "Department Index";
var departments = await _departmentService.GetAll();
return View(departments);
}
public IActionResult Add()//添加Model
{
ViewBag.Title = "Add Department";
return View(new Department());
}
[HttpPost]
public async Task<IActionResult> Add(Department model)//把添加的model提交上来
{
if(ModelState.IsValid)
{
await _departmentService.Add(model);
}
return RedirectToAction(nameof(Index));
}
}
}
namespace Three.Controllers
{
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 employees = await _employeeService.GetDepartmentId(departmentId);
return View(employees);
}
public IActionResult Add(int departmentId)
{
ViewBag.Title = "Add Employee";
return View(new Employee
{
DepartedmentId = departmentId
});
}
[HttpPost]
public async Task<IActionResult> Add(Employee model)
{
if(ModelState.IsValid)
{
await _employeeService.Add(model);
}
return RedirectToAction(nameof(Index), routeValues: new { department = model.DepartedmentId});
}
public async Task<IActionResult> Fire(int employeeId)
{
var employee = await _employeeService.Fire(employeeId);
return RedirectToAction(nameof(Index), routeValues: new { departmentId = employee.DepartedmentId });
}
}
}
6.Views文件→Shared文件→_Layout.cshtml,
Views文件→ ViewImports.html,
Views文件→ ViewStart.cshtml
_Layout.cshtml代码
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
<enviroment include="Development">
<link rel="stylesheet" asp-href-include="css/*" asp-href-exclude="all.min.css" />
</enviroment>
<enviroment exclude="Development">
<link rel="stylesheet" asp-href-include="all.min.css" />
</enviroment>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-2">
<img asp-append-version="true" alt="Logo"
src="~/images/logo.jpg" style="height:60px" />
</div>
<div class="col-md-10">
<span class="h2">@ViewBag.Title</span>
</div>
</div>
<div class="row">
<div class="col-md-12">
@RenderBody()
</div>
</div>
</div>
</body>
</html>
Views文件→Department文件→ 对应DepartmentController里的 Action →Indext.cshtml(表格样式没出来)和Add.cshtml
Views文件→Department文件→DisplayCompanies文件→
Department.cshtml
public async Task<IActionResult> Index()
{
ViewBag.Title = "Department Index";
var departments = await _departmentService.GetAll();
return View(departments);
}
@using Three.Models
@model IEnumerable<Department>
<div class="row">
<div class="col-md-10 offset-md-2">
<table class="table">
<tr>
<th>Name</th>
<th>Location</th>
<th>Employee Count</th>
<th>操作</th>
</tr>
@Html.DisplayForModel()
</table>
</div>
</div>
<div class="row">
<div class="col-md-4 offset-md-2">
<a asp-controller ="Department" asp-aciton="Add">Add</a>
</div>
</div>
※注意引用的model※
public IActionResult Add()//添加Model
{
ViewBag.Title = "Add Department";
return View(new Department());
}
[HttpPost]
public async Task<IActionResult> Add(Department model)//把添加的model提交上来
{
if(ModelState.IsValid)
{
await _departmentService.Add(model);
}
return RedirectToAction(nameof(Index));
}
表单
@using Three.Models
@model Department
<from 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="EmployeeCount"></label>
</div>
<div class="col-md-6">
<input class="form-control" asp-for="EmployeeCount"/>
</div>
</div>
<div class="row">
<div class="col-md-2 offset-md-2">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</div>
</from>
Department.cshtml
@model Three.Models.Department
<tr>
<td>@Model.Name</td>
<td>@Model.Location</td>
<td>@Model.EmployeeCount</td>
<td>
<a asp-controller="Employee" asp-action="Index" asp-route-departmentId="@Model.Id" >Employees</a>
</td>
</tr>
疑问 @Html.DisplayForModel()没有显示出来
Views文件→Employee文件→ 对应EmployeetController里的 Action →Indext.cshtml和Add.cshtml
Views文件→Employee文件→Employees文件→
Employee.cshtml
Indext.cshtml
@using Three.Models
@model IEnumerable<Employee>
<div class="row">
<div class="col-md-10 offset-md-2">
<table class="table">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Gender</th>
<th>Is Fired</th>
<th>操作</th>
</tr>
@Html.DisplayForModel()
</table>
</div>
</div>
<div class="row">
<div class="col-md-4 offset-md-2">
<a asp-action="Add" asp-route-departmentId="@ViewBag.DepartmentId">Add</a>
</div>
</div>
Add.cshtml
@using Three.Models
@model Employee
<from asp-action="Add">
<input type="hidden" asp-for="DepartedmentId"/>
<div class="row from-group">
<div class="col-md-2">
<label asp-for="FirstName"></label>
</div>
<div class="col-md-6">
<input class="from-control" asp-for="FirstName"/>
</div>
</div>
<div class="row from-group">
<div class="col-md-2 offset-md-2">
<label asp-for="LastName"></label>
</div>
<div class="col-md-6">
<input clss="from-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">
<select class="form-control" asp-for="Gender"
asp-items="Html.GetEnumSelectList<Gender>()">
</select>
</div>
</div>
<div class="row">
<div class="col-md-2 offset-md-4">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</div>
</from>
@model Three.Models.Employee
<tr>
<td>@Model.FirstName</td>
<td>@Model.LastName</td>
<td>@Model.Gender</td>
<td>@(Model.Fired?"是":"")</td>
<td>
@if(!Model.Fired)
{
<a asp-action="Fire" asp-route-employeeId="@Model.Id">Fire</a>
}
</td>
</tr>
后面的关于配置文件的课程,看不懂啊看不懂
过