注意:视图:顶部的model一定得是小写;下面用的时候首字母大写
//编辑员工页面
public ActionResult EditEmpForm()
{
//查询部门列表
ViewBag.DeptList = db.Dept;
//查询要编辑的列
ViewBag.emp = db.Employee.Find(int.Parse(Request["EmpId"]));
return View();
}
//修改员工
public ActionResult EditEmp()
{
int EmpId = int.Parse(Request["EmpId"]);
Employee emp = db.Employee.Find(EmpId);
emp.DeptId = int.Parse(Request["DeptId"]);
emp.EmpName = Request["txtName"];
emp.EmpPhone = Request["txtPhone"];
emp.EmpArea = Request["txtArea"];
emp.EmpSalary = decimal.Parse(Request["txtSalary"]);
int count=db.SaveChanges();
if (count > 0)
{
return Content("<script>alert('修改成功');window.location.href='Index'</script>");
}
else
{
return Content("<script>alert('修改失败');window.location.href='Index'</script>");
}
}
视图
引入@Model 项目名.Model.表名
<body>
<form action="~/Home/EditEmp" method="post">
<div style="text-align:center">
<table width="500" align="center" border="1" cellspacing="0">
<input type="hidden" name="EmpId" value="@ViewBag.emp.EmpId" />
<tr>
<td colspan="2"><h3>修改员工</h3></td>
</tr>
<tr>
<td>员工部门</td>
<td>
<select name="DeptId">
<option value="0">--请选择--</option>
@foreach (var item in ViewBag.DeptList)
{
<option value="@item.DeptId" @(item.DeptId==ViewBag.emp.DeptId ? "selected":"")>@item.DeptName</option>
}
</select>
</td>
</tr>
<tr>
<td>员工姓名</td>
<td><input type="text" name="txtName" value="@ViewBag.emp.EmpName"/></td>
</tr>
<tr>
<td>员工电话</td>
<td><input type="text" name="txtPhone" value="@ViewBag.emp.EmpPhone" /></td>
</tr>
<tr>
<td>员工地区</td>
<td><input type="text" name="txtArea" value="@ViewBag.emp.EmpArea" /></td>
</tr>
<tr>
<td>员工薪资</td>
<td><input type="text" name="txtSalary" value="@ViewBag.emp.EmpSalary" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="name" value="修改" /></td>
</tr>
</table>
</div>
</form>
</body>
删除没有前端页面在Index页面中添加
<a href="DeleteEmp?EmpId=@item.EmpId" οnclick="return confirm('确定删除吗?')">删除</a>。