控制器代码: [ HttpGet ] public ActionResult Edit ( int ? id ) { var list = db . tb_user . Where ( a = a . id == id ). FirstOrDefault (); return View ( list ); } [ HttpPost ] public ActionResult Edit ( tb_user u ) { tb_user user = db
控制器代码:
[HttpGet]
public ActionResult Edit(int? id)
{
var list = db.tb_user.Where(a => a.id == id).FirstOrDefault();
return View(list);
}
[HttpPost]
public ActionResult Edit(tb_user u)
{
tb_user user = db.tb_user.Where(v => v.id == u.id).FirstOrDefault();
user.Name = u.Name;
user.Gender = u.Gender;
user.Address = u.Address;
user.Status = u.Status;
user.UpdateTime = u.UpdateTime;
int a=db.SaveChanges();
if (a > 0)
{
return Content("<script>alert('编辑成功');window.location.href='Index'</script>");
}
return Content("<script>alert('编辑失败');window.location.href='Index'</script>");
}
视图
引入@model 项目名.Model.表名
注意:视图:顶部的model一定得是小写;下面用的时候首字母大写
@model net_Manage.Models.tb_user
@{
Layout = null;
}
<link href="~/Content/bootstrap.min.css" rel="stylesheet" />
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>修改-员工疫情信息</title>
</head>
<body>
<div class="container">
<p style="font-size:30px">员工疫情信息修改</p>
<hr />
<form action="/Home/Edit" method="post">
<p>
<input type="hidden" name="id" value="@Model.id" />
</p>
<p>
<label>姓名</label><input type="text" required name="Name" class="form-control" value="@Model.Name" />
</p>
@if (@Model.Gender == "男")
{
<p>
<label>性别</label>
<input type="radio" name="Gender" value="男" checked>男
<input type="radio" name="Gender" value="女" /> 女
</p>
}
else if (@Model.Gender == "女")
{
<p>
<label>性别</label>
<input type="radio" name="Gender" value="男">男
<input type="radio" name="Gender" value="女" checked /> 女
</p>
}
<p>
<label>家庭地址</label><input type="text" name="Address" value="@Model.Address" class="form-control" />
</p>
@if (Model.Status == 0)
{
<p>
<label>当前状态</label>
<select class="form-control" name="Status">
<option value="0" selected>已接种疫苗</option>
<option value="1">感冒状态</option>
<option value="2">从危险地区回来</option>
</select>
</p>
}
else if (Model.Status == 1)
{
<p>
<label>当前状态</label>
<select class="form-control" name="Status">
<option value="0">已接种疫苗</option>
<option value="1" selected>感冒状态</option>
<option value="2">从危险地区回来</option>
</select>
</p>
}
else if (Model.Status == 2)
{
<p>
<label>当前状态</label>
<select class="form-control" name="Status">
<option value="0">已接种疫苗</option>
<option value="1">感冒状态</option>
<option value="2" selected>从危险地区回来</option>
</select>
</p>
}
<p>
<label>录入时间</label><input type="date" name="UpdateTime" class="form-control" value="@Model.UpdateTime.ToString("yyyy-MM-dd")" />
</p>
<p>
<input type="submit" name="name" class="btn btn-default btn-sm" value="修改" />
<a href="/Home/Index">取消</a>
</p>
</form>
</div>
</body>
</html>
删除没有前端页面在Index页面中添加
<a href="DeleteEmp?EmpId=@item.EmpId" οnclick="return confirm('确定删除吗?')">删除</a>。