mvc+linq+EF对数据表的查删改
1 2 3 4 5 6 7 8 9 10 11 12 13 | /// <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(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <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" );} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | @ 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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | @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> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律