mvc+linq+EF对数据表的查删改

时间:2022-12-17 15:35:49
         /// <summary>
/// 查询数据库中学生姓名
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
//使用linq,查询数据上下文中的学生姓名
List<Models.T_student> list = (from d in db.T_student select d).ToList(); //将集合数据传给视图
ViewData["DataList"] = list;
return View();
}

  

         <span style="white-space:pre">	</span>/// <summary>
/// 根据学生ID删除学生
/// </summary>
/// <param name="id">学生ID</param>
/// <returns></returns>
public ActionResult Del(string id)
{
//创建要删除的实体,并将ID赋值给实体对象
T_student modelDel = new T_student() { studentId = id }; //将实体对象添加到EF管理容器
db.T_student.Attach(modelDel); //将实体对象包装类标示为删除状态
db.T_student.Remove(modelDel); //更新数据库
db.SaveChanges(); //更新成功,跳转到Index
return RedirectToAction("Index","MyClass");}

  

#region 显示要修改的数据
[HttpGet]
/// <summary>
/// 显示要修改的数据
/// </summary>
/// <param name="id">要修改的学生ID</param>
/// <returns></returns>
public ActionResult Modify(string id)
{
//根据学生ID,查询数据库,返回集合中拿到第一个实体对象
T_student ts = (from a in db.T_student where a.studentId == id select a).FirstOrDefault(); //查询课程名称
IEnumerable<SelectListItem> listItem=(from c in db.T_class select c).ToList().Select(c=>new SelectListItem{Value=c.classId.ToString(),Text=c.className}); //查询到的课程名称给Viewbag
ViewBag.classList = listItem; //使用View,将数据传给视图上名为model的属性
return View(ts);
}
#endregion #region 保存要修改的数据
[HttpPost]
/// <summary>
/// 保存要修改的数据
/// </summary>
/// <param name="id">要修改的学生ID</param>
/// <returns></returns>
public ActionResult Modify(T_student ts)
{
//将实体对象加入EF对象容器中,并获取包装类对象
DbEntityEntry<T_student> entry=db.Entry<T_student>(ts); //将包装类设置为unchange
entry.State = System.Data.EntityState.Unchanged; //设置被改变的属性
entry.Property(a=>a.studentName).IsModified=true;
entry.Property(a => a.classId).IsModified = true; //提交更新到数据库
db.SaveChanges(); //更新成功,跳转到Index
return RedirectToAction("Index", "MyClass");
}
#endregion

   3.添加查询列表视图(Index.cshtml)

@using MyMvcTest.Models
@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style type="text/css">
#tblist {
border:1px solid #0094ff;
width:600px;
margin:10px auto;
border-collapse:collapse;
}
#tblist th, td {
border:1px solid #0094ff;
padding:10px;
}
</style>
</head>
<body>
<table id="tblist">
<tr>
<th>id</th>
<th>姓名</th>
<th>课程ID</th>
<th>编辑</th>
</tr>
<!--变量action方法 设置viewData的集合数据生成html-->
@foreach (T_student student in ViewData["DataList"] as List<T_student>)
{
<tr>
<td>@student.studentId</td>
<td>@student.studentName</td>
<td>@student.classId</td>
<td>
<a href="/MyClass/del/@student.studentId">删除</a>
<a href="/MyClass/modify/@student.studentId">修改</a>
</td>
</tr>
}
</table> </body>
</html>

  添加“修改”视图(modify.cshtml)

@model MyMvcTest.Models.T_student
@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Modify</title>
<style type="text/css">
#tblist {
border: 1px solid #0094ff;
width: 600px;
margin: 10px auto;
border-collapse: collapse;
} #tblist th, td {
border: 1px solid #0094ff;
padding: 10px;
}
</style>
</head>
<body>
@using (Html.BeginForm("Modify", "MyClass", FormMethod.Post))
{
<table id="tblist">
<tr>
<td colspan="2">修改:@Html.HiddenFor(a=>a.studentId)</td>
</tr>
<tr>
<td>课程名称</td>
<!--使用HtmlHepler,直接从model获取数据赋值给下拉框-->
<td>@Html.DropDownListFor(a => a.classId, ViewBag.classList as IEnumerable<SelectListItem>)</td>
</tr>
<tr>
<td>学生姓名</td>
<!--使用HtmlHepler,直接从model获取数据赋值给文本框-->
<td>@Html.TextBoxFor(a => a.studentName)</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="确定修改">@Html.ActionLink("返回", "Index", "MyClass")</td>
</tr>
</table>
}
</body>
</html>