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 0000000000000000000000000000000000000000..f6ecc6247d94ab7c4dc8d30b7a04905749f75bd5 --- /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 0000000000000000000000000000000000000000..1e544346d67291d828f5877b20ba1b821751dc70 --- /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 0000000000000000000000000000000000000000..039104d4405ff0905a1f6db8e299dfe84eba3a12 --- /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 0000000000000000000000000000000000000000..e3c74be5a400b174cd40b68523327815c898306f --- /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