From d29d3b23b8c516200013b0dde9140c611319a436 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E4=BD=B3=E6=95=8F?= <878083710@qq.com> Date: Sun, 25 Jan 2026 21:21:03 +0800 Subject: [PATCH] 1 --- .../20251231-mvc\347\254\224\350\256\2602.md" | 16 +- ...19-MVC\347\254\224\350\256\260viewmode.md" | 8 + ...56\345\272\223\346\226\260\345\242\236.md" | 153 ++++++++++++++++++ ...56\345\272\223\346\237\245\346\211\276.md" | 13 ++ ...56\345\272\223\347\274\226\350\276\221.md" | 101 ++++++++++++ 5 files changed, 283 insertions(+), 8 deletions(-) rename "\345\274\240\344\275\263\346\225\217/2051231-mvc\347\254\224\350\256\2602.md" => "\345\274\240\344\275\263\346\225\217/20251231-mvc\347\254\224\350\256\2602.md" (98%) create mode 100644 "\345\274\240\344\275\263\346\225\217/20260119-MVC\347\254\224\350\256\260viewmode.md" create mode 100644 "\345\274\240\344\275\263\346\225\217/20260121-mvc\346\225\260\346\215\256\345\272\223\346\226\260\345\242\236.md" create mode 100644 "\345\274\240\344\275\263\346\225\217/20260122-mvc\346\225\260\346\215\256\345\272\223\346\237\245\346\211\276.md" create mode 100644 "\345\274\240\344\275\263\346\225\217/20260123-mvc\346\225\260\346\215\256\345\272\223\347\274\226\350\276\221.md" diff --git "a/\345\274\240\344\275\263\346\225\217/2051231-mvc\347\254\224\350\256\2602.md" "b/\345\274\240\344\275\263\346\225\217/20251231-mvc\347\254\224\350\256\2602.md" similarity index 98% rename from "\345\274\240\344\275\263\346\225\217/2051231-mvc\347\254\224\350\256\2602.md" rename to "\345\274\240\344\275\263\346\225\217/20251231-mvc\347\254\224\350\256\2602.md" index 32fc63d..22d30ec 100644 --- "a/\345\274\240\344\275\263\346\225\217/2051231-mvc\347\254\224\350\256\2602.md" +++ "b/\345\274\240\344\275\263\346\225\217/20251231-mvc\347\254\224\350\256\2602.md" @@ -1,9 +1,9 @@ -## 笔记 -view(视图) -核心职责:1.渲染界面 2.接收用户操作 3.状态展示 4.界面交互控制 5.提供操作接口 -View 的核心特征(避坑关键) -1.无业务逻辑:不处理数据验证、接口请求、复杂计算(这些全丢给 Model); -2.无数据存储:不保存核心数据,渲染所需数据全部由 Controller 传入; -3。仅通知不处理:用户操作后只通知 Controller,不直接调用 Model 方法; -4.纯界面相关:代码只围绕 DOM 操作、样式控制、交互入口,不包含业务逻辑代码; +## 笔记 +view(视图) +核心职责:1.渲染界面 2.接收用户操作 3.状态展示 4.界面交互控制 5.提供操作接口 +View 的核心特征(避坑关键) +1.无业务逻辑:不处理数据验证、接口请求、复杂计算(这些全丢给 Model); +2.无数据存储:不保存核心数据,渲染所需数据全部由 Controller 传入; +3。仅通知不处理:用户操作后只通知 Controller,不直接调用 Model 方法; +4.纯界面相关:代码只围绕 DOM 操作、样式控制、交互入口,不包含业务逻辑代码; 5.高内聚:一个 View 对应一个界面模块(如待办列表 View、用户信息 View),不混杂其他模块的界面逻辑。 \ No newline at end of file diff --git "a/\345\274\240\344\275\263\346\225\217/20260119-MVC\347\254\224\350\256\260viewmode.md" "b/\345\274\240\344\275\263\346\225\217/20260119-MVC\347\254\224\350\256\260viewmode.md" new file mode 100644 index 0000000..d3c17e8 --- /dev/null +++ "b/\345\274\240\344\275\263\346\225\217/20260119-MVC\347\254\224\350\256\260viewmode.md" @@ -0,0 +1,8 @@ +# 笔记 +专门为视图定制的数据载体,解耦视图与业务模型,只传递视图需要的数据 +避免直接暴露业务模型:视图要什么,ViewModel就封装什么 +是Model和View之间的“数据中转站” +1. 纯数据类(一般无业务逻辑),含属性、无复杂方法 +2. 与视图一一对应,一个视图可对应一个专属ViewModel +3. 支持数据验证(如ASP.NET中加[Required]/[MaxLength]等特性) +4. 解耦层与层,视图不直接操作业务Model,提升安全性和可维护性。 \ No newline at end of file diff --git "a/\345\274\240\344\275\263\346\225\217/20260121-mvc\346\225\260\346\215\256\345\272\223\346\226\260\345\242\236.md" "b/\345\274\240\344\275\263\346\225\217/20260121-mvc\346\225\260\346\215\256\345\272\223\346\226\260\345\242\236.md" new file mode 100644 index 0000000..4a9e637 --- /dev/null +++ "b/\345\274\240\344\275\263\346\225\217/20260121-mvc\346\225\260\346\215\256\345\272\223\346\226\260\345\242\236.md" @@ -0,0 +1,153 @@ +## 第一步 + +dotnet new mvc -n Min + +1.在命令行里输入路径到Min--cd .\Min\ +2.安装2个安装包 +dotnet add package Microsoft.EntityframeworkCore.Sqlite -v 8.0 +dotnet add package Microsoft.EntityframeworkCore.Design -v 8.0 +点击Min.csproj查看安装是否成功 + +## 第2步 + +- 在models里面创建一个txt文件然后改名字为products.cs +- 在文件里输入一下代码 + +``` +namespace Min. Models +{ +public class Products +{ + public int Id{get;set;} + public string ProductName {get ;set ;} = null!; + public string? Tag{get ;set ;} + public decimal Price{get ;set ;} +} +} +``` + +- 在models里面在创建一个AppDbContext.cs + 可以dotnet build来查错 + 在文件里输入一下代码 +``` +using Microsoft.EntityFrameworkCore; + +namespace Min.Models; + +public class AppDbContext:DbContext +{ +public DbSet Products{get; set;} +protected override void OnConfiguring(DbContextOptionsBuilder opts) +{ +opts.UseSqlite("Data Source=Min.db"); + +} + +} +``` +- 迁移不成功的情况下需要先安装工具,代码:dotnet tool install --global dotnet-ef +- 再进行数据迁移:dotnet ef migrations add FirstAdd +- 将更新到库:dotnet ef database update + +## 第三步 +在控制器中创建ProductController.cs文件并输入: +``` +using Min.Models; +using Microsoft.AspNetCore.Mvc; + +namespace Min.Controllers; + +public class ProductController : Controller +{ + private readonly AppDbContext db; + public ProductController() + { + db = new AppDbContext(); + + } + + + public IActionResult Index() + { + var list= db.Products.ToList(); + return View(list); + } + public IActionResult Add() + { + return View(); + } + public IActionResult AddSave(Products input) + { + db.Products.Add(input); + db.SaveChanges(); + return Redirect("/product/index"); + } + +} +``` +## 第四步 +在视图View中创建Index.cshtml文件(初始页面) + +``` +@{ + Layout=null; +} +
+添加 +
+ + + + + + + + +@foreach(var item in Model) +{ + + + + + + + + + +} + +
序号商品名称商品标签价格操作
@item.Id@item.ProductName@item.Tag@item.Price +编辑 +删除 +
+``` +## 第五步 +在视图View中再创建一个Add.cshtml文件(新增页面) +``` +@{ + Layout=null; +} + +@model Min.Models.Products; +
+ + + + + + + + + + + + + + + + + +
商品名称:
商品标签:
商品价格:
取消并返回列表
+
+``` + diff --git "a/\345\274\240\344\275\263\346\225\217/20260122-mvc\346\225\260\346\215\256\345\272\223\346\237\245\346\211\276.md" "b/\345\274\240\344\275\263\346\225\217/20260122-mvc\346\225\260\346\215\256\345\272\223\346\237\245\346\211\276.md" new file mode 100644 index 0000000..fe000b1 --- /dev/null +++ "b/\345\274\240\344\275\263\346\225\217/20260122-mvc\346\225\260\346\215\256\345\272\223\346\237\245\346\211\276.md" @@ -0,0 +1,13 @@ +## 查找功能 +在新增代码的基础上 在控制器ProductController.cs中更改 +``` + public IActionResult Index(string keyword) + { + IEnumerable list = db.Products; + if (!String.IsNullOrEmpty(keyword)) + { + list=list.Where(p=>p.ProductName.Contains(keyword)|| p.Tag.Contains(keyword)); + } + return View(list); + } + ``` \ No newline at end of file diff --git "a/\345\274\240\344\275\263\346\225\217/20260123-mvc\346\225\260\346\215\256\345\272\223\347\274\226\350\276\221.md" "b/\345\274\240\344\275\263\346\225\217/20260123-mvc\346\225\260\346\215\256\345\272\223\347\274\226\350\276\221.md" new file mode 100644 index 0000000..a6c26c3 --- /dev/null +++ "b/\345\274\240\344\275\263\346\225\217/20260123-mvc\346\225\260\346\215\256\345\272\223\347\274\226\350\276\221.md" @@ -0,0 +1,101 @@ +# +## Edit.cshtml +``` +@{ + Layout = null; +} + +@model Min.Models.Products; +
+ + + + + + + + + + + + + + + + + + + + + +
商品名称:
商品标签:
商品价格:
取消并返回列表
+
+``` +## ProductController.cs +``` + public IActionResult Edit(int id) + { + var obj=db.Products.Find(id); + if (obj == null) + { + return NotFound(); + } + return View(obj); + } + + public IActionResult EditSave(Products input) + { + var obj=db.Products.Find(input.Id); + if (obj == null) + { + return NotFound(); + } + obj.ProductName=input.ProductName; + obj.Tag=input.Tag; + obj.Price=input.Price; + db.Products.Update(obj); + db.SaveChanges(); + return Redirect("/product/index"); + } +``` +# 删除 +## Del.cshtml +``` +@{ + Layout = null; +} + +@model Min.Models.Products; +

你确认要删除如下商品吗?

+
+    @Model.Id
+    @Model.ProductName
+    @Model.Tag
+    @Model.Price
+
+确认删除 +取消删除 +``` +## ProductController.cs +``` + public IActionResult Del(int id) + { + var obj=db.Products.Find(id); + if (obj == null) + { + return NotFound(); + } + return View(obj); + } + public IActionResult DelSave(int id) + { + var obj=db.Products.Find(id); + if (obj == null) + { + return NotFound(); + } + db.Products.Remove(obj); + db.SaveChanges(); + return Redirect("/product/index"); + } +``` \ No newline at end of file -- Gitee