MVC test
1,index
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 51 | @{ ViewBag.Title = "Index" ; } <!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>状态</th> <th>编辑</th> </tr> <!--变量action方法 设置viewData的集合数据生成html--> @ foreach (test.Models.tb_name item in ViewData[ "DataList" ] as List<test.Models.tb_name>) { <tr> <td>@item.Id</td> <td>@item.uName</td> <td>@item.uMark</td> <td> @Html.ActionLink( "删除" , "Del" , new { id = item.Id }) @Html.ActionLink( "修改" , "Update" , new { id = item.Id }) @Html.ActionLink( "添加" , "Add" ) </tr> } </table> </body> </html> |
2,添加视图
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 51 52 53 54 55 56 57 58 59 60 | @model test.Models.tb_name @{ ViewBag.Title = "Add" ; } <!DOCTYPE html> <html> <head> <meta name= "viewport" content= "width=device-width" /> <title>Add</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> <h2>Create</h2> @ using (Html.BeginForm()) { @Html.ValidationSummary( true ) <fieldset> <legend>Student</legend> <div class = "editor-label" > @Html.LabelFor(model => model.uName) </div> <div class = "editor-field" > @Html.EditorFor(model => model.uName) @Html.ValidationMessageFor(model => model.uName) </div> <div class = "editor-label" > @Html.LabelFor(model => model.uMark) </div> <div class = "editor-field" > @Html.EditorFor(model => model.uMark) @Html.ValidationMessageFor(model => model.uMark) </div> <p> <input type= "submit" value= "Create" /> </p> </fieldset> } <div> @Html.ActionLink( "Back to List" , "Index" ) </div> </body> </html> |
3,修改视图
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 51 52 | @model test.Models.tb_name @{ ViewBag.Title = "Update" ; } <!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( "Update" , "Index" , FormMethod.Post)) { <table id= "tblist" > <tr> <td colspan= "2" >修改:@Html.HiddenFor(a => a.Id)</td> </tr> <tr> <td>课程名称</td> <!--使用HtmlHepler,直接从model获取数据赋值给下拉框--> <td>@Html.DropDownListFor(a => a.Id, ViewBag.classList as IEnumerable<SelectListItem>)</td> </tr> <tr> <td>学生姓名</td> <!--使用HtmlHepler,直接从model获取数据赋值给文本框--> <td>@Html.TextBoxFor(a => a.uName)</td> </tr> <tr> <td colspan= "2" > <input type= "submit" value= "确定修改" >@Html.ActionLink( "返回" , "Index" , "Index" )</td> </tr> </table> } </body> </html> |
4,控制器
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | public class IndexController : Controller { testEntities db = new testEntities(); // // GET: /Index/ public ActionResult Index() { testEntities db = new testEntities(); List<Models.tb_name> list = ( from d in db.tb_name select d).ToList(); ViewData[ "DataList" ] = list; return View(); } /// 根据学生ID删除学生 /// </summary> /// <param name="id">学生ID</param> /// <returns></returns> [HttpGet] public ActionResult Del( string id) { testEntities db = new testEntities(); int ids = Convert.ToInt32(id); tb_name modelDel = new tb_name() { Id = ids }; db.tb_name.Attach(modelDel); db.Entry<tb_name>(modelDel).State = System.Data.EntityState.Deleted; db.SaveChanges(); return RedirectToAction( "Index" , "Index" ); } /// <summary> /// 根据学生编号修改学生 /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpGet] public ActionResult Update( string id) { int ids = Convert.ToInt32(id); tb_name ts = ( from a in db.tb_name where a.Id == ids select a).FirstOrDefault(); IEnumerable<SelectListItem> listItem = ( from c in db.tb_name select c).ToList().Select(c => new SelectListItem { Value = c.Id.ToString(), Text = c.uName }); ViewBag.classList = listItem; return View(ts); } [HttpPost] /// <summary> /// 保存要修改的数据 /// </summary> /// <param name="id">要修改的学生ID</param> /// <returns></returns> public ActionResult Update(tb_name ts) { DbEntityEntry<tb_name> entry = db.Entry<tb_name>(ts); entry.State = System.Data.EntityState.Unchanged; entry.Property(a => a.uName).IsModified = true ; entry.Property(a => a.Id).IsModified = true ; db.SaveChanges(); return RedirectToAction( "Index" , "Index" ); } public ActionResult Add() { return View( "Add" ); } /// <summary> /// 添加 /// </summary> /// <returns></returns> [HttpPost] public ActionResult Add(tb_name student) { try { if (ModelState.IsValid) { EntityState statebefore = db.Entry(student).State; //Detached db.tb_name.Add(student); EntityState stateAdd = db.Entry(student).State; //Added db.SaveChanges(); EntityState stateafter = db.Entry(student).State; //Unchanged return RedirectToAction( "Index" ); } } catch { ModelState.AddModelError( "" , "Unable to save changes. Try again, and if the problem persists see your system administrator." ); } return View(student); } } |
【推荐】国内首个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 绘制太阳,地球,月球 运作规律