一,控制器代码
//主页
// GET: Huawei
public ActionResult Index()
{
huaweiEntities db = new huaweiEntities();
ViewBag.Emps = db.Employee.ToList();
return View();
}
//登录
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(string username,string userpwd)
{
huaweiEntities db = new huaweiEntities();
var empResult = db.Employee.SingleOrDefault(p => p.Name == username && p.EmployeeID.ToString() == userpwd);
if (empResult != null)
{
//取消登录按钮,来一个退出按钮和登录者名字
return RedirectToAction("index");
}
else
{
return View();
}
}
//详情
public ActionResult resotion()
{
var id = Request.QueryString["id"];
huaweiEntities db = new huaweiEntities();
ViewBag.resotion= db.Employee.SingleOrDefault(p => p.EmployeeID.ToString() == id);
return View();
}
//删除
public void Delete(string id)
{
huaweiEntities db = new huaweiEntities();
var a= db.Employee.SingleOrDefault(p => p.EmployeeID.ToString() == id);
db.Employee.Remove(a);//删除
if (db.SaveChanges() > 0)
{
Response.Write("<script>alert('删除成功');location.href='/Huawei/index'</script>");
}
}
//添加
public ActionResult Add(string id)
{
huaweiEntities db = new huaweiEntities();
ViewBag.add1 = db.Rank.ToList();
ViewBag.add2 = db.Dept.ToList();
return View();
}
[HttpPost]
public void Add2(Employee employee,HttpPostedFileBase file)
{
huaweiEntities db = new huaweiEntities();
//路径找不到一部分?
file.SaveAs(Server.MapPath("~/"+file.FileName));
employee.imgUrl = file.FileName;
employee.hit = 0;
employee.status = 1;
db.Employee.Add(employee);
db.SaveChanges();
}
//修改
public ActionResult Update(Employee employee,string id)
{
huaweiEntities db = new huaweiEntities();
ViewBag.emp= db.Employee.SingleOrDefault(p => p.EmployeeID.ToString() == id);
ViewBag.rank = db.Rank.ToList();
ViewBag.dept = db.Dept.ToList();
return View();
}
[HttpPost]
public ActionResult Update(Employee employee, HttpPostedFileBase file)
{
huaweiEntities db = new huaweiEntities();
file.SaveAs(Server.MapPath("~/"+file.FileName));
db.SaveChanges();
return View();
}
二,前台代码
主页:
<a href="/Huawei/Add">添加员工</a>
<table class="table table-striped text-nowrap">
<thead>
<tr>
<td class="a">编号</td>
<td class="a">姓名</td>
<td class="a">性别</td>
<td class="a">薪水</td>
<td class="a">生日</td>
<td class="a">部门</td>
<td class="a">职位</td>
<td class="a">地址</td>
<td class="a">国家</td>
<td class="a">头像</td>
<td class="a">点击量</td>
<td class="a">状态</td>
<td class="a">操作</td>
</tr>
</thead>
<tbody>
@foreach (var item in ViewBag.Emps)
{
<tr>
<td class="b">@item.EmployeeID</td>
<td class="b">@item.Name</td>
<td class="b">@item.Gender</td>
<td class="b">@item.Salary</td>
<td class="b">@item.Birthday</td>
<td class="b">@item.Rank.rankname1</td>
<td class="b">@item.Dept.deptName</td>
<td class="b">@item.Address </td>
<td class="b">@item.Country</td>
<td class="b"><img width="130" height="80" src="/@item.imgUrl" alt="" /> </td>
<td class="b">@item.hit</td>
<td class="b">@(item.status == 0 ? "在职" : "离职")</td>
<td class="b"><a href="/Huawei/resotion?id=@item.EmployeeID">详情</a> <a href="#">编辑</a> <a onclick="return confirm('确定要删除吗?')" href="/Huawei/Delete?id=@item.EmployeeID">删除</a></td>
</tr>
}
</tbody>
</table>
添加页:
<h2 class="text-center">华为添加页</h2>
<form action="/Huawei/Add2" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="col-sm-2 control-label">用户名:</label>
<input class="form-control" name="Name" id="Text1" type="text" />
</div>
<div class="form-group">
<label class="col-sm-2 control-label">性别:</label>
<select>
<option name="Gender" selected>男</option>
<option name="Gender">女</option>
</select>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">薪水:</label>
<input class="form-control" name="Salary" id="Text1" type="text" />
</div>
<div class="form-group">
<label class="col-sm-2 control-label">生日:</label>
<input class="form-control" name="Birthday" id="Text1" type="date" />
</div>
<div class="form-group">
<label class="col-sm-2 control-label">部门:</label>
<select name="Dept.deptName ">
@foreach (var item in ViewBag.add1)
{
<option>@item.rankname1</option>
}
</select>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">职位:</label>
<select name="Dept.deptName ">
@foreach (var item in ViewBag.add2)
{
<option>@item.deptName</option>
}
</select>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">地址:</label>
<input class="form-control" name="Address" id="Text1" type="text" />
</div>
<div class="form-group">
<label class="col-sm-2 control-label">国家:</label>
<input class="form-control" name="Country" id="Text1" type="text" />
</div>
<div class="form-group">
<label class="col-sm-2 control-label">头像:</label>
<input id="File1" name="file" onchange="reimg.src = window.URL.createObjectURL(this.files[0])" type="file" />
</div>
<div class="form-group">
<input id="Button1" type="submit" value="添加" />
<br />
<a href="/Huawei/Index">返回主页</a>
</div>
</form>
修改页
<h2 class="text-center">修改员工</h2>
<form action="/Huawei/Update" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="col-sm-2 control-label">编号:</label>
<input class="form-control" value="@ViewBag.emp.EmployeeID" name="EmployeeID" id="Text1" type="text" readonly />
</div>
<div class="form-group">
<label class="col-sm-2 control-label">用户名:</label>
<input class="form-control" value="@ViewBag.emp.Name" name="Name" id="Text1" type="text" />
</div>
<div class="form-group">
<label class="col-sm-2 control-label">性别:</label>
<p>
@if (ViewBag.emp.Gender == "男")
{
<input type="radio" name="Gender" value="@ViewBag.emp.Gender" checked /><lable>男</lable>
<input type="radio" name="Gender" value="@ViewBag.emp.Gender" /><lable>女</lable>
}
else
{
<input type="radio" name="Gender" value="@ViewBag.emp.Gender" /><lable>男</lable>
<input type="radio" name="Gender" value="@ViewBag.emp.Gender" checked /><lable>女</lable>
}
</p>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">薪水:</label>
<input class="form-control" value="@ViewBag.emp.Salary" name="Salary" id="Text1" type="text" />
</div>
<div class="form-group">
<label class="col-sm-2 control-label">生日:</label>
<input class="form-control" value="@ViewBag.emp.Birthday.ToString("yyyy-MM-dd")" name="Birthday" id="Text1" type="date" />
</div>
<div class="form-group">
<label class="col-sm-2 control-label">部门:</label>
<select name="DeptID">
@foreach (var item in ViewBag.dept)
{
if (ViewBag.emp.DeptID == item.deptID)
{
<option value="@item.deptID" selected>@item.deptName</option>
}
else
{
<option value="@item.deptID">@item.deptName</option>
}
}
</select>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">职位:</label>
<select name="RankID">
@foreach (var item in ViewBag.rank)
{
if (ViewBag.emp.RankID == item.RankID)
{
<option value="@item.RankID" selected>@item.rankname1</option>
}
else
{
<option value="@item.RankID">@item.rankname1</option>
}
}
</select>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">地址:</label>
<input class="form-control" value="@ViewBag.emp.Address" name="Address" id="Text1" type="text" />
</div>
<div class="form-group">
<label class="col-sm-2 control-label">国家:</label>
<input class="form-control" value="@ViewBag.emp.Country" name="Country" id="Text1" type="text" />
</div>
<div class="form-group">
<label class="col-sm-2 control-label">头像:</label>
<input id="File1" name="file" value="@ViewBag.emp.imgUrl" onchange="reimg.src = window.URL.createObjectURL(this.files[0])" type="file" />
</div>
<input type="hidden" name="hit" value="@ViewBag.emp.hit" />
<input type="hidden" name="status" value="@ViewBag.emp.status" />
<div class="form-group">
<input id="Button1" type="submit" value="修改" />
<br />
<a href="/Huawei/Index">返回主页</a>
</div>
</form>
详情页
<h2 class="text-center">华为详情页</h2>
<p>编号:<label>@ViewBag.resotion.EmployeeID</label></p>
<p>姓名:<label>@ViewBag.resotion.Name</label></p>
<p>性别:<label>@ViewBag.resotion.Gender</label></p>
<p>薪水:<label>@ViewBag.resotion.Salary</label></p>
<p>生日:<label>@ViewBag.resotion.Birthday</label></p>
<p>部门:<label>@ViewBag.resotion.Dept.deptName</label></p>
<p>职位:<label>@ViewBag.resotion.Rank.rankname1 </label></p>
<p>地址:<label>@ViewBag.resotion.Address</label></p>
<p>国家:<label>@ViewBag.resotion.Country</label></p>
<p>头像:<label>@ViewBag.resotion.imgUrl</label></p>
<p>点击量:<label>@ViewBag.resotion.hit</label></p>
<p>状态:<label>@ViewBag.resotion.status</label></p>
<p><a href="/Huawei/index">返回首页</a></p>
登录页
<div class="col-md-offset-0">
<h2 class="col-lg-offset-2">登录</h2>
<form class="form-horizontal" action="/huawei/login" method="post">
<div class="form-group">
<label class="col-sm-2 control-label">用户名:</label>
<div class="col-sm-10">
<input class="form-control" name="username" id="Text1" type="text" />
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">密码:</label>
<div class="col-sm-10">
<input class="form-control" name="userpwd" id="Text1" type="text" />
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input id="Button1" type="submit" value="登录" />
</div>
</div>
</form>
</div>
三,数据库
链接: 数据库
提取码: fanc
四,项目
连接: 项目
提取码: fanc