diff --git "a/\344\276\257\345\207\244\351\230\263/20241125(\350\277\224\345\233\236\345\200\274)\347\254\224\350\256\260.md" "b/\344\276\257\345\207\244\351\230\263/20241125(\350\277\224\345\233\236\345\200\274)\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..00ded61480202f2f63b9a9ce0dfba7d2f70c8fe9 --- /dev/null +++ "b/\344\276\257\345\207\244\351\230\263/20241125(\350\277\224\345\233\236\345\200\274)\347\254\224\350\256\260.md" @@ -0,0 +1,15 @@ +- 返回值常见数据类型 ,如 int,string,List<> + - ICtionRresult,返回响应状态码,如:200.301,401,404,500等 + - 视图 + - 重定向 + - ActionResult<> , 可以同时返回状态码或者常规数据类型 + - JsonResult ,ContentResult ,返回响应就是存粹的数据 + - POCO 可以返回一个对象,而这个对象就在被返回的时候,会被序列化(相对的是,反系列化) + ## 常用状态码和返回类型总结 +| 状态码 | 含义 | 返回类型 | +| ------- | ------------------------ | ---------------------- | +| 200 | 请求成功,数据返回 | `IActionResult` / `ViewResult` / `JsonResult` | +| 301 | 永久重定向,资源已移动 | `RedirectResult` | +| 401 | 未授权,身份验证失败 | `UnauthorizedResult` | +| 404 | 资源未找到 | `NotFoundResult` | +| 500 | 服务器内部错误 | `StatusCodeResult` | \ No newline at end of file diff --git "a/\344\276\257\345\207\244\351\230\263/20241127(\350\247\206\345\233\276)\347\254\224\350\256\260.md" "b/\344\276\257\345\207\244\351\230\263/20241127(\350\247\206\345\233\276)\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..1d7a2e5928548c6e2a9e231522af1720625cc4cf --- /dev/null +++ "b/\344\276\257\345\207\244\351\230\263/20241127(\350\247\206\345\233\276)\347\254\224\350\256\260.md" @@ -0,0 +1,28 @@ +# Razor知识点 +``` +Razor 支持 C#,并使用@符号从 HTML 转换为 C#。 Razor 计算 C# 表达式,并将它们呈现在 HTML 输出中。 +当 @ 符号后跟 Razor 保留关键字时,它会转换为 Razor 特定标记. +隐式 Razor 表达式 +以 @ 开头,后跟 C# 代码。隐式表达式不能包含空格,但 C# await 关键字除外。不能包含 C# 泛型,因为括号 (<>) 内的字符会被解释为 HTML 标记。 +显式 Razor 表达式 +由 @ 符号和平衡圆括号组成。 +``` +# 定位点标记帮助程序 + +``` +Razor 还支持通过 asp- 前缀扩展 HTML 标记,使其支持动态生成 URL。常见的标记包括: + +asp-controller +asp-controller 属性指定用于生成 URL 的控制器名称。例如,列出所有发言人的链接可以写作: + +All Speakers +asp-action +asp-action 属性指定生成的 URL 中的控制器操作。例如,跳转到发言人评估页: + +Speaker Evaluations +asp-route-{value} +asp-route-{value} 用于动态生成带有路由参数的 URL。{value} 会被替换为实际的路由参数值。例如: + +Speaker Details +如果匹配不到路由模板,asp-route-{value} 会被当作请求参数附加到生成的 URL。 +``` \ No newline at end of file diff --git "a/\344\276\257\345\207\244\351\230\263/20241129\347\254\224\350\256\260.md" "b/\344\276\257\345\207\244\351\230\263/20241129\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..f00e0a67dcfd868361725c50d0cc7f33239e06a1 --- /dev/null +++ "b/\344\276\257\345\207\244\351\230\263/20241129\347\254\224\350\256\260.md" @@ -0,0 +1,128 @@ +# 定义一个模型Models,模拟数据库 +定义一个数据库的表 +静态构造函数(只执行一次,内容如果修改,则需要重新跑) +初始化静态属性 - 直接初始化:public static List Blog = new List{"value1","value2"...}; - 使用静态构造函数(下面例子中的方法,推荐使用该方法 - 延迟初始化 - 使用属性初始化器 +给这个集合属性,塞进去一些内容 +## 创建数据库模型 +``` +public static class Db +{ + public static List Blog { get; set; } + + // 静态构造函数,确保初始化只执行一次 + static Db() + { + Blog = new List(); + + for (int i = 0; i < 15; i++) + { + var tmp = new Blog + { + Id = i + 1, + Title = "Title " + (i + 1), + Content = "Content of blog post " + (i + 1), + Author = "Author " + (i + 1) + }; + Blog.Add(tmp); + } + } +} +``` +静态构造函数:Db 类的静态构造函数会在第一次访问类时执行,并初始化 Blog 集合。 +延迟初始化:这确保了 Blog 集合只有在需要时才被初始化。 +数据填充:通过静态构造函数,模拟的数据库会自动填充 15 条数据。 +## 创建视图来展示内容 +1. 展示博客列表并提供增、删、改功能 +2. 使用 标签和 Razor 的 asp- 属性来生成控制器操作的链接。以新增博客为例: + +``` +新增 +asp-controller:指定表单提交的目标控制器。 +asp-action:指定表单提交时调用控制器中的动作方法。 +``` +### 编辑链接示例 +编辑 +asp-route-id:传递给控制器的路由参数,通常是编辑项的 ID。 +3. 跳转至增加页面并创建表单 +创建新增博客的表单 +在 Create 动作对应的视图中,使用 form 表单来提交数据: + +@model Blogs.Models.Blog + +
+
+
+
+ +
+@model 声明:指定当前视图使用的模型类型。 +asp-for:为表单控件指定与模型属性的绑定。 +asp-action="Create":表单提交后将调用 Create 动作方法。 +4. 使用 POST 请求添加数据 +处理新增请求 +在控制器的 Create 动作中,我们将接收表单数据并将其添加到模拟数据库中。 + +[HttpPost] +public IActionResult Create(Blog input) +{ + // 获取表单中最大 ID 数,确保新添加的博客有唯一 ID + var maxId = Db.Blog.Select(t => t.Id).Max(); + input.Id = maxId + 1; // 设置新的 ID + + // 将新增的博客添加到数据库 + Db.Blog.Add(input); + + // 重定向到博客列表页面 + return RedirectToAction("Index"); + // 测试:return Content(JsonSerializer.Serialize(input)); 可返回提交的内容 +} +获取最大 ID:使用 Max() 方法来找到当前 Blog 集合中的最大 ID,以便新博客的 ID 能自动递增。 +重定向:数据添加后,页面会重定向回博客列表页 (Index),确保用户可以看到最新的博客列表。 + + +# 作业: + + +渲染简单数据到页面:c:\Users\ASUS\Desktop\大二第三门\grade23-class3-note-mvc\侯凤阳\img\渲染图片\简单渲染1.png![alt text](简单渲染1.png) +``` + public int Index(){ + return 994 ; + } + ``` + +2. 渲染复杂数据到页面 +![alt text](复杂渲染1.png) + + ``` + public dynamic Ho() + { + return new {Name="你好,陈梦涵先生"}; + } +``` + +3. 渲染集合数据到页面 +![alt text](复杂集合渲染.png) +``` +public List He() + { + var list = new List + { + "挂科", + "大二", + "第一门", + "11" + }; + return list; + } +``` + +4. 渲染(展示)简单数据类型到视图 +![alt text](渲染简单视图2.png) +``` + public string Hw() + { + return "我在看西北岁月电视剧"; + } + +``` + diff --git "a/\344\276\257\345\207\244\351\230\263/img/\346\270\262\346\237\223\345\233\276\347\211\207/\345\244\215\346\235\202\346\270\262\346\237\2231.png" "b/\344\276\257\345\207\244\351\230\263/img/\346\270\262\346\237\223\345\233\276\347\211\207/\345\244\215\346\235\202\346\270\262\346\237\2231.png" new file mode 100644 index 0000000000000000000000000000000000000000..9b953039ee341536efea8f171b2039ff535ec120 Binary files /dev/null and "b/\344\276\257\345\207\244\351\230\263/img/\346\270\262\346\237\223\345\233\276\347\211\207/\345\244\215\346\235\202\346\270\262\346\237\2231.png" differ diff --git "a/\344\276\257\345\207\244\351\230\263/img/\346\270\262\346\237\223\345\233\276\347\211\207/\345\244\215\346\235\202\351\233\206\345\220\210\346\270\262\346\237\223.png" "b/\344\276\257\345\207\244\351\230\263/img/\346\270\262\346\237\223\345\233\276\347\211\207/\345\244\215\346\235\202\351\233\206\345\220\210\346\270\262\346\237\223.png" new file mode 100644 index 0000000000000000000000000000000000000000..ce98deeb0a6c970f53f482e2100811530e574edc Binary files /dev/null and "b/\344\276\257\345\207\244\351\230\263/img/\346\270\262\346\237\223\345\233\276\347\211\207/\345\244\215\346\235\202\351\233\206\345\220\210\346\270\262\346\237\223.png" differ diff --git "a/\344\276\257\345\207\244\351\230\263/img/\346\270\262\346\237\223\345\233\276\347\211\207/\346\270\262\346\237\223\347\256\200\345\215\225\350\247\206\345\233\2762.png" "b/\344\276\257\345\207\244\351\230\263/img/\346\270\262\346\237\223\345\233\276\347\211\207/\346\270\262\346\237\223\347\256\200\345\215\225\350\247\206\345\233\2762.png" new file mode 100644 index 0000000000000000000000000000000000000000..e3b172f2a934d949e1b6a10ff0f29c0a080c07d9 Binary files /dev/null and "b/\344\276\257\345\207\244\351\230\263/img/\346\270\262\346\237\223\345\233\276\347\211\207/\346\270\262\346\237\223\347\256\200\345\215\225\350\247\206\345\233\2762.png" differ diff --git "a/\344\276\257\345\207\244\351\230\263/img/\346\270\262\346\237\223\345\233\276\347\211\207/\347\256\200\345\215\225\346\270\262\346\237\2231.png" "b/\344\276\257\345\207\244\351\230\263/img/\346\270\262\346\237\223\345\233\276\347\211\207/\347\256\200\345\215\225\346\270\262\346\237\2231.png" new file mode 100644 index 0000000000000000000000000000000000000000..6ded355b13fc984f6ae307b6bae8227d08ebbeb7 Binary files /dev/null and "b/\344\276\257\345\207\244\351\230\263/img/\346\270\262\346\237\223\345\233\276\347\211\207/\347\256\200\345\215\225\346\270\262\346\237\2231.png" differ