diff --git "a/\347\216\213\351\235\226\350\214\271/20241122-MVC\347\254\254\344\270\211\346\254\241\350\257\276\345\240\202\347\254\224\350\256\260.md" "b/\347\216\213\351\235\226\350\214\271/20241122-MVC\347\254\254\344\270\211\346\254\241\350\257\276\345\240\202\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..5425e2140b91be6b5df1a2ede3f5a383c44dda2d --- /dev/null +++ "b/\347\216\213\351\235\226\350\214\271/20241122-MVC\347\254\254\344\270\211\346\254\241\350\257\276\345\240\202\347\254\224\350\256\260.md" @@ -0,0 +1,91 @@ +# 准备软件包升级 + ++ 安装可用更新 + ++ 自动解决依赖关系 + ++ 提高系统安全性 + ++ 保持系统稳定 + ++ 自动化和一致性 + ++ 维护软件的兼容性 + ++ 获取新功能和改进 + +# 更新软件和补丁 + +执行命令:apt upgrade -y + ++ 了解这一步的实际用处意义:确保系统和软件包是最新的,以修复已知的安全漏洞和提升性能。 + +# 传参 + +## 简单传参(单个) + +``` +public IActionResult Edit(int id){ + return Content(id.ToString()); +} +``` + ++ 如有多个简单传参形式,则需修改Program.cs中的pattern里的值,例如: + +``` +pattern: "{controller=Home}/{action=Index}/{age?}"; +public IActionResult Edit(int age){ + return Content(age.ToString()); +} +``` + +## 复杂传参 + +### 第一步 + +``` +[HttpPost] +public IActionResult Create([FromBody]Students students){ + return Content(JsonSerializer.Serialize(students)); +} +``` + ++ 该方式需创建视图中创建一个与控制器相对应对视图文件(书写样式时可用),但还是在Postman软件中查看(前提要运行该程序F5)。 + +### 第二步 + +``` +// [HttpPost] +public IActionResult Create([FromBody]Students students){ + return View(students); +} +``` + ++ 需创建一个类,这个类里有属性: + +``` +public class Students{ + public string Name{get;set;}=null!; + public string Age{get;set;}=null!; + public string Weight{get;set;}=null!; + public string Height{get;set;}=null!; +} +``` + +第三步 + ++ 因该方法在浏览器中无法查看,需到Postman这个软件中查看结果。 + +## 在控制器中构造对象并传递给视图 + +``` +public IActionResult Index(){ + var students=new Students{ + Name="零零", + Age="33", + Weight="55", + Height="34" + }; + return View(students); +} +``` \ No newline at end of file diff --git "a/\347\216\213\351\235\226\350\214\271/\346\235\202\344\270\203\346\235\202\345\205\253\344\270\234\350\245\2772/mvc\347\273\203\344\271\240.txt" "b/\347\216\213\351\235\226\350\214\271/\346\235\202\344\270\203\346\235\202\345\205\253\344\270\234\350\245\2772/mvc\347\273\203\344\271\240.txt" new file mode 100644 index 0000000000000000000000000000000000000000..505f594f2b339f49b9893c139230bc48986341c9 --- /dev/null +++ "b/\347\216\213\351\235\226\350\214\271/\346\235\202\344\270\203\346\235\202\345\205\253\344\270\234\350\245\2772/mvc\347\273\203\344\271\240.txt" @@ -0,0 +1,213 @@ +1. 创建一个控制台项目,没有任何选项,体会项目名称和什么有关系 +dotnet new console -n MyConsoleApp + +2. 创建一个控制台项目,项目名称为 Blog +dotnet new console -n Blog + +3. 创建一个控制台项目,输出到 Blog 目录 +dotnet new console -o Blog + +4. 创建一个 MVC 项目,指定项目名称 +dotnet new mvc -n MyMVCApp + +5. 创建一个 MVC 项目,指定输出目录 +dotnet new mvc -o MyMVCApp + +6. 创建一个带解决方案,其下有一个 MVC 项目,3 个类库项目的“综合项目” +dotnet new sln -n ComprehensiveProject + +dotnet new mvc -n MyMVCApp +dotnet sln ComprehensiveProject.sln add MyMVCApp/MyMVCApp.csproj + +dotnet new classlib -n LibraryA +dotnet sln ComprehensiveProject.sln add LibraryA/LibraryA.csproj + +dotnet new classlib -n LibraryB +dotnet sln ComprehensiveProject.sln add LibraryB/LibraryB.csproj + +dotnet new classlib -n LibraryC +dotnet sln ComprehensiveProject.sln add LibraryC/LibraryC.csproj + +解决方案名称:ComprehensiveProject.sln +MVC 项目:MyMVCApp +类库项目:LibraryA, LibraryB, LibraryC + +7. 在默认控制器(Home)下,新增一个 Action 方法,名为 Ok,同时为其创建对应视图以显示这个视图 + +public IActionResult Ok() +{ + return View(); +} + +创建视图 +@{ + ViewData["Title"] = "Ok"; +} + +
这是一个新增的 Ok 视图。
+ +运行 +dotnet run --project MyMVCApp/MyMVCApp.csproj + +8. 创建一个新的控制器,名为 Blogs,新的控制器中添加一个名为 Index 的 Action,该方法返回一个视图,视图显示“神级预判” + +创建控制器: +在 MyMVCApp/Controllers/ 目录下创建一个新的控制器文件 BlogsController.cs,添加以下内容: +using Microsoft.AspNetCore.Mvc; + +namespace MyMVCApp.Controllers +{ + public class BlogsController : Controller + { + public IActionResult Index() + { + ViewBag.Message = "神级预判"; + return View(); + } + } +} + +创建视图: +在 MyMVCApp/Views/Blogs/ 目录下创建一个新的视图文件 Index.cshtml,添加以下内容: + +@{ + ViewData["Title"] = "Blogs"; +} + +博客标题 | +
---|
@blog | +
Product ID: @ViewBag.ProductId
+ +12. 在11题的新控制器中,新增一个名为 Create 的 Action,该 Action 接受一个类型为 Students(有姓名、年龄、体长属性)的参数,并展示该参数的姓名属性 + +定义 Students 类: +在 MyMVCApp/Models/ 目录下创建新的类文件 Student.cs,添加 + +namespace MyMVCApp.Models +{ + public class Student + { + public string Name { get; set; } + public int Age { get; set; } + public double Height { get; set; } + } +} + +修改 ProductsController: +在 ProductsController.cs 中添加 +using MyMVCApp.Models; + +public IActionResult Create(Student student) +{ + if (student == null) + { + return BadRequest(); + } + + ViewBag.StudentName = student.Name; + return View(); +} + +创建视图: +在 MyMVCApp/Views/Products/ 目录下创建新的视图文件 Create.cshtml + +@model MyMVCApp.Models.Student + +@{ + ViewData["Title"] = "Create Student"; +} + +Student Name: @ViewBag.StudentName
\ No newline at end of file