diff --git "a/\351\273\204\351\233\252\350\212\254/20241122-\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" "b/\351\273\204\351\233\252\350\212\254/20241122-\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" index b05a852baa4db0918c3d6e8bea3dcc07ef24fa8a..ef41301e0e4d3d0e6e1e87124060506f038bd8ab 100644 --- "a/\351\273\204\351\233\252\350\212\254/20241122-\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" +++ "b/\351\273\204\351\233\252\350\212\254/20241122-\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202.md" @@ -51,6 +51,7 @@ public ActionResult Create(MyModel model) 使用数据绑定技术,将请求参数自动映射到控制器方法的参数上。例如,在Spring MVC中,可以使用@RequestParam注解来绑定查询参数,使用@PathVariable注解来绑定路径参数。 # 作业 -![1](./作业/屏幕截图%202024-11-24%20220538.png) -![2](./作业/屏幕截图%202024-11-24%20220626.png) -![3](./作业/屏幕截图%202024-11-24%20220638.png) \ No newline at end of file +## mvc练习 +![1](./imgs/mvc练习%20(1).png) +![2](./imgs/mvc练习%20(2).png) +![3](./imgs/mvc练习%20(3).png) \ No newline at end of file diff --git "a/\351\273\204\351\233\252\350\212\254/20241125-\350\277\224\345\233\236\345\200\274\347\261\273\345\236\213.md" "b/\351\273\204\351\233\252\350\212\254/20241125-\350\277\224\345\233\236\345\200\274\347\261\273\345\236\213.md" new file mode 100644 index 0000000000000000000000000000000000000000..714fbd46b086fe990b4b1d07ce54146a9a550403 --- /dev/null +++ "b/\351\273\204\351\233\252\350\212\254/20241125-\350\277\224\345\233\236\345\200\274\347\261\273\345\236\213.md" @@ -0,0 +1,91 @@ +## MVC模式中常见的Action的返回值类型 +1. 常见数据类型,如int、string、IList<> +```cs +public IActionResult Index() +{ + var list = new List + { + new BlogCreateDto + { + Title = "今天下雨", + Author = "今天下雨", + Content = "今天下雨", + }, + new BlogCreateDto + { + Title = "今天下雨", + Author = "今天下雨", + Content = "今天下雨", + }, + new BlogCreateDto + { + Title = "今天下雨", + Author = "今天下雨", + Content = "今天下雨", + }, + }; + return View(); +} + +public class BlogCreateDto +{ + public string Title{get;set;} = null!; + public string Author{get;set;} = null!; + public string Content{get;set;} = null!; +} +``` +```html + +@model List + + + + + + + + + @foreach (var item in Model) + { + + + + + + + } +
标题作者内容操作
@item.Title@item.Author@item.Content + + +
+``` + +2. IActionResult,响应状态码,如200(请求成功,并返回了)、301、401、404(资源找不到)、500(服务器错误)等 + - 视图:除了访问该视图,还可以访问指定名称的视图 return View("视图名称") + - 重定向:return RedirectToAction("视图名称"); + +3. ActionREsult<>,可以同时返回状态或者常规数据类型 +```cs +public ActionResult Edit() +{ + var isOk = 1 == 3 ? true : false ; + if (isOk) + { + return View(); + } + else + { + return "5432"; + } +} +``` + +4. JsonResult、ContentResult,返回响应,就是纯粹的数据 + +5. POCO,可以返回一个对象,而该对象在被返回的时候会被序列化(相对应的是 反序列化--Json,相当于js的对象) + + +# 作业 +## 控制器传参 +![1](./imgs/控制器传参(1).png) +![2](./imgs/控制器传参%20(2).png) \ No newline at end of file diff --git "a/\351\273\204\351\233\252\350\212\254/20241127-\351\232\217\346\234\272\346\225\260.md" "b/\351\273\204\351\233\252\350\212\254/20241127-\351\232\217\346\234\272\346\225\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..6b17a8e93eb8c622f87c60da0a37a5350581c461 --- /dev/null +++ "b/\351\273\204\351\233\252\350\212\254/20241127-\351\232\217\346\234\272\346\225\260.md" @@ -0,0 +1,185 @@ +# 作业 +## 基础能力随机数 +### 1 +```cs +//生成一个随机整数,范围[0,100],注意是否包含 + public IActionResult Index() + { + Random random = new Random(); + int ranNum = random.Next(0, 101); + return View(ranNum); + } +``` +### 2 +```cs +//生成一个随机整数,范围(0,100],注意是否包含 + public IActionResult Index_1() + { + Random random = new Random(); + int ranNum = random.Next(1, 101); + return View(ranNum); + } +``` +### 3 +```cs +//生成10个随机整数,范围[5,80],注意是否包含 + public IActionResult Index_2() + { + Random random = new Random(); + List ints = new List(); + for (int i = 0; i < 10; i++) + { + int randNum = random.Next(5, 81); + ints.Add(randNum); + } + return View(ints); + } +``` +### 4 +```cs +//定义一个字符串,字符串中有100个中文字符,需要从中随机取1个字符串 + public IActionResult Index_3() + { + string str = "的是他她它们有无在不了和与为上下前后左右中大小多少高低好坏美丑快慢老少新旧生死强弱软硬长短方圆扁平直曲黑白红黄蓝绿紫灰橙粉金银水火木土风雨雪雷电云山河海天地星月阳光爱恨情仇快乐悲伤怒喜即积稳存耀贺"; + Random random = new Random(); + char y = str[random.Next(str.Length)]; + return View(y); + } +``` +### 5 +```cs +//定义一个字符串,字符串中有100个中文字符,需要从中随机取5-50个字符,组成新的字符 + public IActionResult Index_4() + { + string str = "的是他她它们有无在不了和与为上下前后左右中大小多少高低好坏美丑快慢老少新旧生死强弱软硬长短方圆扁平直曲黑白红黄蓝绿紫灰橙粉金银水火木土风雨雪雷电云山河海天地星月阳光爱恨情仇快乐悲伤怒喜即积稳存耀贺"; + Random random = new Random(); + int h = random.Next(5, 51); + string arr = ""; + for (int i = 0; i < h; i++) + { + int index = random.Next(str.Length); + arr += str[index]; + } + ViewBag.Arr = arr; + return View(); + } +``` +### 6 +```cs +//定义2个字符串,第一个字符串中放百家姓,第二个字符串中放中文字符,要求从第一个字符串随机取得一个姓,再从第二个字符串中随机获得1到2个字符组成新字符串,和第一个字符串取得的姓组成一个姓名 + public IActionResult Index_5() + { + string xing = "赵,钱,孙,李,周,吴,郑,王,冯,陈,褚,卫,蒋,沈,韩,杨,朱,秦,尤,许,孔,曹,严,华,金,魏,陶,姜,戚,谢,邹,喻,柏,水,窦,章,云,苏,潘,葛,奚,范,彭,郎,鲁,韦,昌,马,苗,凤,花,方,俞,任,袁,柳,酆,鲍,史,唐,费,廉,岑,薛,雷,贺,倪,汤,滕,殷,罗,毕,郝,邬,安,常,乐,于,时,傅,皮,卞,齐,康,伍,余,元,卜,顾,孟,平,黄,和,穆,萧,尹,姚,邵,湛,汪,祁"; + string chinese = "的一是了不在有和人我大个这中上来为国以子地你家他时生为命发下对说去就而子那好可其会"; + Random random = new Random(); + string[] fname = xing.Split(','); + string surname = fname[random.Next(fname.Length)]; + int count = random.Next(1, 3); + string arrname = ""; + for (int i = 0; i < count; i++) + { + int index = random.Next(chinese.Length); + arrname += chinese[index]; + } + string name = surname + arrname; + ViewBag.name = name; + return View(); + } +``` +## 控制器返回值 +```cs + public int Index() + { + return 777; + } + public dynamic Edit() + { + return new { Name = "修马" }; + } + public dynamic List() + { + var list = new List + { + "hhh", + "xxx", + "yyy" + }; + return list; + } +``` +![1](./imgs/控制器返回值%20(1).png) +![2](./imgs/控制器返回值%20(2).png) +![3](./imgs/控制器返回值%20(3).png) +## 视图及其模板引擎 +```cs + public IActionResult Aaa() + { + var list=new List + { + new BogCreateDto + { + ID="1", + Title="33333", + Content="9999", + Author="" + }, + new BogCreateDto + { + ID="2", + Title="33333", + Content="9999", + Author="" + }, + new BogCreateDto + { + ID="3", + Title="33333", + Content="9999", + Author="" + }, + new BogCreateDto + { + ID="4", + Title="33333", + Content="9999", + Author="" + } + }; + return View(list); + } + +public class BogCreateDto +{ + public string ID{get;set;}=null!; + public string Title{get;set;}=null!; + public string Content{get;set;}=null!; + public string Author{get;set;}=null!; +} +``` +```html +@model List + + + 新增 + + + + + + @foreach(var item in Model) + { + + + + + + + } +
ID标题内容作者
@item.ID@item.Title@item.Content + @item.Author + 编辑 | + 详情 | + 删除 +
+``` +![](./imgs/列表.png) \ No newline at end of file diff --git "a/\351\273\204\351\233\252\350\212\254/20241129-\345\242\236\346\223\215\344\275\234.md" "b/\351\273\204\351\233\252\350\212\254/20241129-\345\242\236\346\223\215\344\275\234.md" new file mode 100644 index 0000000000000000000000000000000000000000..12712da6eb61e175a29de31446a61dab8002fc66 --- /dev/null +++ "b/\351\273\204\351\233\252\350\212\254/20241129-\345\242\236\346\223\215\344\275\234.md" @@ -0,0 +1,71 @@ +## 实现增操作 +### 使用post请求,添加到表单 +1. 获取表单中最大的ID数,,方便程序在添加中自行增加Id数:`Max()方法` +2. 测试添加内容是否有被获取到:`return Content(JsonSerializer.Serialize(input))` +3. 将内容添加至表单 +```cs +[HttpPost] +public IActionResult Create(Blog input) +{ + var maxId = Db.Blog.Select(t => t.Id).Max(); + input.Id = maxId + 1; + Db.Blog.Add(input); + return RedirectToAction("Index"); +} +``` +### 定义一个模型Models,模拟数据库 +1. 定义一个数据库的表 +2. 静态构造函数(只执行一次,内容如果修改,则需要重新跑) + 1) 初始化静态属性 + - 直接初始化:`public static List Blog = new List{"value1","value2"...};` + - 使用静态构造函数(下面例子中的方法,推荐使用该方法 + - 延迟初始化 + - 使用属性初始化器 + 2) 给这个集合属性,塞进去一些内容 +```cs +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,... + }; + + Blog.Add(tmp); + } + } +} +``` + +### 创建视图,展示内容 +1. 使用``标签展现 增删改 + ```html + 新增 + ``` +2. `asp-controller`:指定表单提交的目标控制器 +3. `asp-action`:指定表单提交时应该调用的控制器中的动作(方法) +4. `asp-route-*`:通常与以上两个属性配合使用,便在创建表单或链接时能够自动将模型的状态或路由参数传递给控制器的特定动作。-id就是传递相应行的id号 + ```html + 编辑 + ``` + +### 跳转至增加页面 +1. 页面显示form表单,一定要声明Models +```html + +@model Blogs.Models.Blog; + +
+
+
+
+ +
+``` diff --git "a/\351\273\204\351\233\252\350\212\254/\344\275\234\344\270\232/\345\261\217\345\271\225\346\210\252\345\233\276 2024-11-24 220538.png" "b/\351\273\204\351\233\252\350\212\254/imgs/mvc\347\273\203\344\271\240 (1).png" similarity index 100% rename from "\351\273\204\351\233\252\350\212\254/\344\275\234\344\270\232/\345\261\217\345\271\225\346\210\252\345\233\276 2024-11-24 220538.png" rename to "\351\273\204\351\233\252\350\212\254/imgs/mvc\347\273\203\344\271\240 (1).png" diff --git "a/\351\273\204\351\233\252\350\212\254/\344\275\234\344\270\232/\345\261\217\345\271\225\346\210\252\345\233\276 2024-11-24 220626.png" "b/\351\273\204\351\233\252\350\212\254/imgs/mvc\347\273\203\344\271\240 (2).png" similarity index 100% rename from "\351\273\204\351\233\252\350\212\254/\344\275\234\344\270\232/\345\261\217\345\271\225\346\210\252\345\233\276 2024-11-24 220626.png" rename to "\351\273\204\351\233\252\350\212\254/imgs/mvc\347\273\203\344\271\240 (2).png" diff --git "a/\351\273\204\351\233\252\350\212\254/\344\275\234\344\270\232/\345\261\217\345\271\225\346\210\252\345\233\276 2024-11-24 220638.png" "b/\351\273\204\351\233\252\350\212\254/imgs/mvc\347\273\203\344\271\240 (3).png" similarity index 100% rename from "\351\273\204\351\233\252\350\212\254/\344\275\234\344\270\232/\345\261\217\345\271\225\346\210\252\345\233\276 2024-11-24 220638.png" rename to "\351\273\204\351\233\252\350\212\254/imgs/mvc\347\273\203\344\271\240 (3).png" diff --git "a/\351\273\204\351\233\252\350\212\254/imgs/\345\210\227\350\241\250.png" "b/\351\273\204\351\233\252\350\212\254/imgs/\345\210\227\350\241\250.png" new file mode 100644 index 0000000000000000000000000000000000000000..cf714f365d2fd5c47fd3569618b8b9201861c1b7 Binary files /dev/null and "b/\351\273\204\351\233\252\350\212\254/imgs/\345\210\227\350\241\250.png" differ diff --git "a/\351\273\204\351\233\252\350\212\254/imgs/\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202 (2).png" "b/\351\273\204\351\233\252\350\212\254/imgs/\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202 (2).png" new file mode 100644 index 0000000000000000000000000000000000000000..2cbe7f0889eb7d193b9159dc60d2b519bd408c56 Binary files /dev/null and "b/\351\273\204\351\233\252\350\212\254/imgs/\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202 (2).png" differ diff --git "a/\351\273\204\351\233\252\350\212\254/imgs/\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202(1).png" "b/\351\273\204\351\233\252\350\212\254/imgs/\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202(1).png" new file mode 100644 index 0000000000000000000000000000000000000000..077ad28911efbd9d22b5ae3be905cf99f017a87f Binary files /dev/null and "b/\351\273\204\351\233\252\350\212\254/imgs/\346\216\247\345\210\266\345\231\250\344\274\240\345\217\202(1).png" differ diff --git "a/\351\273\204\351\233\252\350\212\254/imgs/\346\216\247\345\210\266\345\231\250\350\277\224\345\233\236\345\200\274 (1).png" "b/\351\273\204\351\233\252\350\212\254/imgs/\346\216\247\345\210\266\345\231\250\350\277\224\345\233\236\345\200\274 (1).png" new file mode 100644 index 0000000000000000000000000000000000000000..512862e5687a04100c894a4e65183fc07e1eba7b Binary files /dev/null and "b/\351\273\204\351\233\252\350\212\254/imgs/\346\216\247\345\210\266\345\231\250\350\277\224\345\233\236\345\200\274 (1).png" differ diff --git "a/\351\273\204\351\233\252\350\212\254/imgs/\346\216\247\345\210\266\345\231\250\350\277\224\345\233\236\345\200\274 (2).png" "b/\351\273\204\351\233\252\350\212\254/imgs/\346\216\247\345\210\266\345\231\250\350\277\224\345\233\236\345\200\274 (2).png" new file mode 100644 index 0000000000000000000000000000000000000000..42c6565f4701ae04b7f336f17b171d9cbd550baf Binary files /dev/null and "b/\351\273\204\351\233\252\350\212\254/imgs/\346\216\247\345\210\266\345\231\250\350\277\224\345\233\236\345\200\274 (2).png" differ diff --git "a/\351\273\204\351\233\252\350\212\254/imgs/\346\216\247\345\210\266\345\231\250\350\277\224\345\233\236\345\200\274 (3).png" "b/\351\273\204\351\233\252\350\212\254/imgs/\346\216\247\345\210\266\345\231\250\350\277\224\345\233\236\345\200\274 (3).png" new file mode 100644 index 0000000000000000000000000000000000000000..04ad797061d1b4a3452acddcf6aaef1b2716e5b0 Binary files /dev/null and "b/\351\273\204\351\233\252\350\212\254/imgs/\346\216\247\345\210\266\345\231\250\350\277\224\345\233\236\345\200\274 (3).png" differ