From 17e6436a55fa4cc91ca0f4d144bfbc5be691ec78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=AF=E5=B9=BF=E6=81=BA?= <1332727966@qq.com> Date: Sun, 18 Jan 2026 21:43:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20260112.md" | 10 ++++++ .../20260114.md" | 31 ++++++++++++++++ .../20260115.md" | 19 ++++++++++ .../20260116.md" | 35 +++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 "\345\206\257\345\271\277\346\201\272/20260112.md" create mode 100644 "\345\206\257\345\271\277\346\201\272/20260114.md" create mode 100644 "\345\206\257\345\271\277\346\201\272/20260115.md" create mode 100644 "\345\206\257\345\271\277\346\201\272/20260116.md" diff --git "a/\345\206\257\345\271\277\346\201\272/20260112.md" "b/\345\206\257\345\271\277\346\201\272/20260112.md" new file mode 100644 index 0000000..f6ecc62 --- /dev/null +++ "b/\345\206\257\345\271\277\346\201\272/20260112.md" @@ -0,0 +1,10 @@ +默认路由规则(在 Program.cs 中配置): +```C# +app.MapControllerRoute( +name: "default", +pattern: "{controller=Home}/{action=Index}/{id?}"); +``` +模板解释: +- {controller}:控制器名(去掉Controller后缀) +- {action}:动作方法名 +- {id?}:可选参数,? 表示可选 \ No newline at end of file diff --git "a/\345\206\257\345\271\277\346\201\272/20260114.md" "b/\345\206\257\345\271\277\346\201\272/20260114.md" new file mode 100644 index 0000000..1e54434 --- /dev/null +++ "b/\345\206\257\345\271\277\346\201\272/20260114.md" @@ -0,0 +1,31 @@ +MVC新增功能实现: +关键:必须做前后端双重数据校验,确保数据合法性,新增后需给用户明确的结果反馈。 +```C# +public async Task Create([Bind("Id,Name,Age")] Student student)| +{ +if (ModelState.IsValid) // 断点1:检查模型验证是否通过| +{ +_context.Add(student); // 断点2:检查要新增的student数据| +await _context.SaveChangesAsync(); // 断点3:执行数据库保存| +return RedirectToAction(nameof(Index)); +} +return View(student); +} +``` +MVC删除功能实现: +关键:优先增加确认环节防误删,推荐使用 “软删除”,删除操作必须基于唯一主键,同时处理关联数据的异常情况。 +```C# +public async Task Delete(int? id) +{ +if (id == null \_context.Student == null) +{ +return NotFound(); +} +var student = await _context.Student.FirstOrDefaultAsync(m => m.Id == id); +if (student == null) +{ +return NotFound(); +} +return View(student); +} +``` \ No newline at end of file diff --git "a/\345\206\257\345\271\277\346\201\272/20260115.md" "b/\345\206\257\345\271\277\346\201\272/20260115.md" new file mode 100644 index 0000000..039104d --- /dev/null +++ "b/\345\206\257\345\271\277\346\201\272/20260115.md" @@ -0,0 +1,19 @@ +查找功能的实现: +```C# + // 查找功能 + + // IsNullOrEmpty方法用于判断字符串是否为null或空字符串("") + + // AsQueryable方法将一个IEnumerable转换为IQueryable + + public ActionResult Index(string searchName = "") + { + var res = vipList.AsQueryable(); + if (!string.IsNullOrEmpty(searchName)) + { + res = res.Where(v => v.Name.Contains(searchName)); + } + return View(res.ToList()); + + } +``` \ No newline at end of file diff --git "a/\345\206\257\345\271\277\346\201\272/20260116.md" "b/\345\206\257\345\271\277\346\201\272/20260116.md" new file mode 100644 index 0000000..e3c74be --- /dev/null +++ "b/\345\206\257\345\271\277\346\201\272/20260116.md" @@ -0,0 +1,35 @@ +编辑功能的实现: +```C# + // 编辑功能 + public ActionResult Edit(int id) + { + var vip = vipList.FirstOrDefault(v => v.id == id); + if (vip == null) + { + return NotFound(); + } + return View(vip); + } + + [HttpPost] + public ActionResult Edit(Vipinfo vip) + { + // ModelState.IsValid用于模型绑定和验证是否通过 + if (ModelState.IsValid) + { + var existingVip = vipList.FirstOrDefault(v => v.id == vip.id); + if (existingVip != null) + { + existingVip.Name = vip.Name; + + existingVip.Birthday = vip.Birthday; + + existingVip.Score = vip.Score; + + existingVip.Gender = vip.Gender; + } + return RedirectToAction("index"); + } + return View(vip); + } +``` \ No newline at end of file -- Gitee