diff --git "a/\350\222\213\347\276\244/20241125-mvc\345\270\270\350\247\201\350\277\224\345\233\236\345\200\274\347\261\273\345\236\213.md" "b/\350\222\213\347\276\244/20241125-mvc\345\270\270\350\247\201\350\277\224\345\233\236\345\200\274\347\261\273\345\236\213.md" new file mode 100644 index 0000000000000000000000000000000000000000..e72cfa0dab86078d7a582bd95fa6a6424603d825 --- /dev/null +++ "b/\350\222\213\347\276\244/20241125-mvc\345\270\270\350\247\201\350\277\224\345\233\236\345\200\274\347\261\273\345\236\213.md" @@ -0,0 +1,10 @@ +### mvc常见Action的返回值 + + - 返回常见的数据类型,如int,string,Ilist<> + - IactionResult,返回响应状态码,如200,301,401(代表打不开),500(打得开)等 + - 视图 + - 重定向 + + - Actionresult<>,可以同时返回状态码或者常规数据类型 + - JsonResult,ContenResult,返回响应式就是纯粹的数据 + - POCO可以返回一个对象,而这个对象在被返回的时候,会被序列化(相对于的是 反序列化) \ No newline at end of file diff --git "a/\350\222\213\347\276\244/20241127-\346\216\247\345\210\266\345\231\250\350\277\224\345\233\236\345\200\274.md" "b/\350\222\213\347\276\244/20241127-\346\216\247\345\210\266\345\231\250\350\277\224\345\233\236\345\200\274.md" new file mode 100644 index 0000000000000000000000000000000000000000..2d728652713d13724305c987a7627af5ae69bffc --- /dev/null +++ "b/\350\222\213\347\276\244/20241127-\346\216\247\345\210\266\345\231\250\350\277\224\345\233\236\345\200\274.md" @@ -0,0 +1,157 @@ +### 专项练习生成随机数相关练习讲解 + + +### 控制器返回值 + +1. 渲染简单数据到页面 + +2. 渲染复杂数据到页面 + +3. 渲染集合数据到页面 + + +### 作业1 +#### 10个静态网站 +图片:![20241201190010](sjt78aar6.hn-bkt.clouddn.com/20241201190010.png) +### 作业2 +#### 专项训练--控制器传参 +```js +using Blog.Models; +using Microsoft.AspNetCore.Mvc; +namespace Blog.Controllers; +using System.Text.Json; + + +public class ActionController : Controller{ + +//1.简单参数传递 在一个叫Blog控制器中,定义一个叫Index的Action,并且传递一个int类型的值,id为变量名 + public IActionResult Index(int id){ + return Content(id.ToString()); + + + } + //2.简单参数传递 在一个叫Blog控制器中,定义一个叫Index_2的Action,并且传递一个string类型的值,id为变量名 + public IActionResult Index_2(string id){ + return View(); + + + } + //3.简单参数传递 在一个叫Blog控制器中,定义一个叫Index_3的Action,并且传递一个string类型的值,name为变量名 + public IActionResult Index_3(string name){ + return View(); + + + } + //4.复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create的Action,并且传递一个BlogCreateDto类型的值,blogCreateDto为变量名 + +// PS BlogCreateDto类型具有Title、Author、Content自动属性 + + [HttpPost] +public IActionResult Create([FromBody] BlogCreateDto blogCreateDto) +{ + return Content(blogCreateDto.Title); +} +} +public class BlogCreateDto{ + public string Title{get;set;}=null!; + public string Author{get;set;}=null!; + public string Content{get;set;}=null!; + + //5.复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create_1的Action,并且传递一个Products类型的值,productCreateDto为变量名 + +// PS Products类型具有Name、Price、Stock自动属性 + public class BlogContriller : Controller{ + [HttpPost] + public IActionResult Create_2([FromBody] Products productCreateDto){ + return Content(productCreateDto.Name); + } + } + public class Products{ + public string Name{get;set;}=null!; + public string Price{get;set;}=null!; + public string Stock{get;set;}=null!; + } +//6.复杂参数传递 在一个叫Blog的控制器中,定义一个名为Create_2的Action,并且传递一个Students类型的值,studentCreateDto为变量名 + +// PS Students类型具有StudentName、Sex、Age自动属性 + +} + + public class BlogContriller : Controller{ + [HttpPost] + public IActionResult Create_2([FromBody] Students studentCreateDto){ + return Content(studentCreateDto.Name); + } + } + public class Students{ + public string Name{get;set;}=null!; + public string Sex{get;set;}=null!; + public string Age{get;set;}=null!; + } +``` + + +### 作业2 +#### 随机数 +```js +using Microsoft.AspNetCore.Mvc; +namespace Blog.Controllers; + +public class BlogsController:Controller{ + +public IActionResult Index1(){ +// 1. 生成一个随机整数,范围[0,100],注意是否包含 + + //new一个随机数 + Random random=new Random(); + //生成0-100之间的数 + int randomNumber=random.Next(0,101); + + // 将随机数传递给视图 + ViewBag.RandomNumber = randomNumber; + + return View(); +} + +public IActionResult Index2() + { + // 2.生成一个随机整数,范围(0,100],注意是否包含 + + // new一个随机数 + Random random = new Random(); + // 生成随机数 + int randomNumber = random.Next(1,101); + // 使用ViewBag传递到视图 + ViewBag.RandomNumber = randomNumber; + + return View(); + } + + public List Index3() + { + // 3.生成10个随机整数,范围[5,80],注意是否包含 + + // 随机数 + var random = new Random(); + // runNUm存储10个数 + int runNUm; + // 创建一个集合保存随机数 + var list = new List(); + + // 使用循环 10个随机数 + for(int i=0;i<10;i++){ + // 将10个随机数存储到runNUm中 + runNUm = random.Next(5,81); + // 再将runNUm添加到集合中 + list.Add(runNUm); + + } + + + return list; + } + +} + +``` + diff --git "a/\350\222\213\347\276\244/20241129-\350\247\206\345\233\276\346\270\262\346\237\223.md" "b/\350\222\213\347\276\244/20241129-\350\247\206\345\233\276\346\270\262\346\237\223.md" new file mode 100644 index 0000000000000000000000000000000000000000..8b6cd224ee82401af584c0fd163811941d759663 --- /dev/null +++ "b/\350\222\213\347\276\244/20241129-\350\247\206\345\233\276\346\270\262\346\237\223.md" @@ -0,0 +1,325 @@ +### 视图显示相关操作 + +1. 先创建一个新的文件夹 dotnet new mvc -o Blog +2. 切换文件夹 cd +3. 在Controllers控制器里加入新的文件名 BlogsController.cs +4. 添加相关的命名空间和方法 + +相关BlogsControllers.cs的代码 +```js +using Blog.Models; + +using Microsoft.AspNetCore.Mvc; +namespace Blog.Controllers; + +public class BlogsController:Controller{ + + public IActionResult Index() + { + return View(Db.Blogs); + } + public IActionResult Create() + { + return View(); + } + [HttpPost] + public IActionResult Create(Blogs input) + { + var maxId=Db.Blogs.Select(t=>t.Id).Max(); + input.Id=maxId+1; + Db.Blogs.Add(input); + + + return RedirectToAction("Index"); + } + public IActionResult Edit() + { + return View(); + } + public IActionResult Delete() + { + return View(); + } +} + +``` + +5. 在Models里添加文件 Blogs.cs +相关代码 +```js +namespace Blog.Models; + +public class Blogs{ + + public int Id{get;set;} + public string Title{get;set;}=null!; + public string Content{get;set;}=null!; + public string Author{get;set;}=null!; + +} +``` + +6. 继续在Models里添加新的文件 Db.cs +相关代码 +```js +namespace Blog.Models; + +//静态类型模拟一个表 +public static class Db{ + public static ListBlogs{get;set;} + //静态构造函数,只执行一次 + static Db(){ + //初始化静态属性 + Blogs=new List(); + + //给这个集合属性,塞进去一些Blogs + + for (int i = 0; i < 15; i++) + { + var tmp=new Blogs{ + + Id=i+1, + Title="今晚早点睡", + Content="早睡早起身体好", + Author="蒋蒋蒋" + }; + Blogs.Add(tmp); + } + } + +} +``` + +7. 视图里添加文件夹,命名空间和Blogs一样,里面在加 相关方法.cshtml +Index.cshtml相关代码 +```js +@model List; + +
+ +
+ @* *@ + + 新增 +
+ + + + + + + + + + + + + + + @foreach(var blog in @Model){ + + + + + + + + + + + } + + + +
Id标题内容作者操作
@blog.Id@blog.Title@blog.Content@blog.Author + @* + *@ + 编辑 + 删除 +
+ +
+ +``` + +```js +//Create方法相关代码 +@model Blog.Models.Blogs; +
+
+
+
+ +
+ +//继续添加要跳转文件的方法.cshtml + +Delete.cshtml //删除 + +Edit.cshtml //编辑 + + + +``` +8.想要样式的话 在wwwroot里css中添加一个 命名.css的文件,里面可以像html一样可以写样式 + +是在Index.cs里引用这个链接 +```js + +``` + + +9. 每一次要重跑一下:dotnet watch + +### 作业1 +#### 控制器返回值 +```js +using Microsoft.AspNetCore.Mvc; +namespace Blog.Controllers; +public class BlogsController : Controller{ + +//渲染简单数据到页面 + public int Index() + { + return 777; + } + //渲染复杂数据到页面 + public dynamic Fz() + { + return new{Name="你好aaaaaaa"}; + } + + //渲染集合数据到页面 + public List Jhe() + { + var list = new List + { + "早睡早起", + "今晚早点睡", + "睡个美容觉" + + }; + return list; + } + +} +``` + +### 作业2 + + +效果图:![20241201185201](sjt78aar6.hn-bkt.clouddn.com/20241201185201.png) +相关代码: +```js +//BlogsController.cs +using Blog.Models; +using Microsoft.AspNetCore.Mvc; +namespace Blog.Controllers; +public class BlogsController : Controller{ + + public IActionResult Index(){ + return View(Db.Blogs); + } + public IActionResult Edit(){ + return View(); + } + public IActionResult Xqing(){ + return View(); + } + public IActionResult Delete(){ + return View(); + } +} +``` +```js +//Models ->Blogs.cs +namespace Blog.Models; +public class Blogs{ + + public int Id{get;set;} + + public string Title{get;set;}=null!; + public string Content{get;set;}=null!; + public string Author{get;set;}=null!; +} +//Db.cs +namespace Blog.Models; + +public static class Db{ + public static ListBlogs{get;set;} + + //静态构造函数,只执行一次 + static Db(){ + Blogs=new List(); + + //给集合属性塞东西 + for (int i = 0; i < 5; i++) + { + var tmp=new Blogs{ + Id=i+1, + Title="33333", + Content="9999", + Author="s" + }; + Blogs.Add(tmp); + } + } +} + +``` +```js +//Views +Delete.cshtml +Edit.cshtml +```js +Index.cshtml +@model List; + + +
+ +
+ 新增 + +
+
+ + + + + + + + + + + @foreach(var blog in @Model){ + + + + + @* *@ + + + } + + +
ID标题内容作者
@blog.Id@blog.Title@blog.Content@blog.Author + 编辑 | + 详情 | + 删除 + +
+ +
+ +
+ +``` + + +Xqing.cshtml +``` + + +