diff --git a/readme.md b/readme.md index afe258797b50f7d30e1aa239c313d07833d67b3e..968e08a631dbd7caa5866f35626112d7d56bf3d6 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -## 23级软件5班 MVC课堂笔记 -上课录屏地址:https://www.alipan.com/s/HMxZbQeNagN - -石可破也,而不可夺坚;丹可磨也,而不可夺赤。《吕氏春秋.诚廉》 \ No newline at end of file +## 23级软件5班 MVC课堂笔记 +上课录屏地址:https://www.alipan.com/s/HMxZbQeNagN + +石可破也,而不可夺坚;丹可磨也,而不可夺赤。《吕氏春秋.诚廉》 \ No newline at end of file diff --git "a/\346\254\247\351\230\263\344\271\276/1.PNG" "b/\346\254\247\351\230\263\344\271\276/1.PNG" new file mode 100644 index 0000000000000000000000000000000000000000..2283b2d4a6e753add1322797a90f7964ecdf3073 Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/1.PNG" differ diff --git "a/\346\254\247\351\230\263\344\271\276/12.03.PNG" "b/\346\254\247\351\230\263\344\271\276/12.03.PNG" new file mode 100644 index 0000000000000000000000000000000000000000..f86a6b14598dc2a589285b40dcb78cb166daf724 Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/12.03.PNG" differ diff --git "a/\346\254\247\351\230\263\344\271\276/2.PNG" "b/\346\254\247\351\230\263\344\271\276/2.PNG" new file mode 100644 index 0000000000000000000000000000000000000000..ed6157ad1e640fc56ae02d4fc5721426fea44418 Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/2.PNG" differ diff --git "a/\346\254\247\351\230\263\344\271\276/20241114\347\254\224\350\256\260.md" "b/\346\254\247\351\230\263\344\271\276/20241114\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..e6916aa955956647f36d54d9c3efc02b3aeebcfd --- /dev/null +++ "b/\346\254\247\351\230\263\344\271\276/20241114\347\254\224\350\256\260.md" @@ -0,0 +1,70 @@ +## 创建MVC项目 + +### 1. 安装.NET SDK + +使用以下命令检查是否已安装: +```bash +dotnet --version +``` + +### 2. 创建一个新的 MVC 项目 + +```bash +dotnet new mvc -n 项目名称 +``` + + +### 3. 进入项目目录 +创建完成后,进入你的项目目录: + +```bash +cd MyMvcApp +``` + +### 4. 运行项目 +运行你的MVC项目,启动开发服务器: + +```bash +dotnet run +``` + +### 5. 项目结构 +创建的 MVC 项目会包含以下目录和文件: + +- **Controllers**:控制器类,负责处理用户的请求并返回响应。 + +- **Views**:视图文件,使用 Razor 语法来呈现 HTML 页面。 + +- **Models**:用于存放数据模型的类。 + +- **wwwroot**:包含静态文件,如 CSS、JavaScript 和图片。 + +- **appsettings.json**:应用程序的配置文件。 + +- **Program.cs**:配置和启动应用程序的入口点。 + +### 访问控制器和视图 + + +- ### 最基本的控制器结构 + + ```csharp + csharp复制代码using Microsoft.AspNetCore.Mvc; + + namespace MyMvcApp.Controllers + { + // 控制器类继承自 Controller 基类 + public class HomeController : Controller + { + // 最简单的操作方法,返回视图 + public IActionResult Index() + { + return View(); + } + } + } + ``` + + + + diff --git "a/\346\254\247\351\230\263\344\271\276/20241115\347\254\224\350\256\260.md" "b/\346\254\247\351\230\263\344\271\276/20241115\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..15c59e8607231c9e0c4b37acaa1c09bbb978242b --- /dev/null +++ "b/\346\254\247\351\230\263\344\271\276/20241115\347\254\224\350\256\260.md" @@ -0,0 +1,11 @@ +# 解决方案 +(创建语法 : dotnet new sln -n 解决方案名) +src文件夹(里面的子级文件统称项目) +mvc项目(一个 | 创建语法 : dotnet new mve -o mvc项目名) +类库(多个 | 创建语法 : dotnet new calsslib -o 类库名) + +# 解决方案与项目绑定 +语法:dotnet sln add 绑定项目的路径 + +# 启动 +启动:dotnet run 关闭:ctrl + c \ No newline at end of file diff --git "a/\346\254\247\351\230\263\344\271\276/20241119.png" "b/\346\254\247\351\230\263\344\271\276/20241119.png" new file mode 100644 index 0000000000000000000000000000000000000000..1fdb89132517009d8c81b0aea696906034ad7dbe Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/20241119.png" differ diff --git "a/\346\254\247\351\230\263\344\271\276/20241119\347\254\224\350\256\260.md" "b/\346\254\247\351\230\263\344\271\276/20241119\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..341fc22ee59ec8796c2910dc819bd89b28912193 --- /dev/null +++ "b/\346\254\247\351\230\263\344\271\276/20241119\347\254\224\350\256\260.md" @@ -0,0 +1,21 @@ +# 登陆服务器 +1、ssh root@域名 +2、安装nginx apt install -y ninx +3、测试 systemctl status nginx +4、安装sdk +更新软件源 apt update +创建一个新的文件夹 +命令:mkdir [文件夹名] +查看当前目录下的文件和文件夹 +命令:ls +打包程序 +dotnet publish +配置 Nginx 反向代理 +server { + listen 80; + server_name 域名或者IP; + location / { + proxy_pass http://localhost:5000; + } +} +检查是否有语法错误:nginx -t 重新加载配置:nginx -s reload \ No newline at end of file diff --git "a/\346\254\247\351\230\263\344\271\276/20241121\347\254\224\350\256\260.md" "b/\346\254\247\351\230\263\344\271\276/20241121\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..83aef68840119588620db3d099947b3a572d1a1e --- /dev/null +++ "b/\346\254\247\351\230\263\344\271\276/20241121\347\254\224\350\256\260.md" @@ -0,0 +1,57 @@ +# 通过路由传递参数: +在控制器的Action中,你可以通过在路由模板中定义参数来接收参数。 + +``` GET: Home/Details/5 +public IActionResult Details(int id) +{ + var item = _context.Items.FirstOrDefault(m => m.Id == id); + return View(item); +} +``` +# 通过表单传递参数: +当使用表单提交数据时,可以通过模型绑定将表单字段绑定到Action方法的参数。 +``` +public class Item +{ + public int Id { get; set; } + public string Name { get; set; } +} +``` +``` +// POST: Home/Create +[HttpPost] +public IActionResult Create(Item item) +{ + // 保存item对象到数据库 +} +``` +# 通过FromBody传递参数: +对于使用HTTP POST或PUT方法提交的JSON或XML数据,可以使用[FromBody]属性来接收数据。 + + +``` +[HttpPost] +public IActionResult Create([FromBody] Item item) +{ + // 保存item对象到数据库 +} +``` +# 通过查询字符串传递参数: +当参数通过URL的查询字符串传递时,你可以在Action方法中通过参数接收它们。 + +``` +// GET: Home/Details?name=John&age=30 +public IActionResult Details(string name, int age) +{ + // 使用name和age参数 +} +``` +# 通过Header传递参数: +``` +可以通过[FromHeader]属性来获取HTTP请求头中的参数。 + +public IActionResult GetSecretData([FromHeader] string secretKey) +{ + // 根据secretKey参数执行操作 +} +``` \ No newline at end of file diff --git "a/\346\254\247\351\230\263\344\271\276/20241122.1.png" "b/\346\254\247\351\230\263\344\271\276/20241122.1.png" new file mode 100644 index 0000000000000000000000000000000000000000..2b596e5c3aaf97d791e1048c50c1df1e39c1245b Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/20241122.1.png" differ diff --git "a/\346\254\247\351\230\263\344\271\276/20241122.md" "b/\346\254\247\351\230\263\344\271\276/20241122.md" new file mode 100644 index 0000000000000000000000000000000000000000..33474b38eb6f4707336d46935a7172483dfd5313 --- /dev/null +++ "b/\346\254\247\351\230\263\344\271\276/20241122.md" @@ -0,0 +1,84 @@ +# 渲染复杂数据到页面 +模型 (Models/ComplexData.cs) +public class ComplexData +{ + public string Name { get; set; } + public int Age { get; set; } + public List Hobbies { get; set; } +} +控制器 (Controllers/HomeController.cs) +using System.Collections.Generic; +using System.Web.Mvc; +using YourNamespace.Models; + +public class HomeController : Controller +{ + public ActionResult ComplexData() + { + var model = new ComplexData + { + Name = "John Doe", + Age = 30, + Hobbies = new List { "Reading", "Traveling", "Swimming" } + }; + return View(model); + } +} +视图 (Views/Home/ComplexData.cshtml) +@model YourNamespace.Models.ComplexData + +

Name: @Model.Name

+

Age: @Model.Age

+

Hobbies:

+ +3. 渲染集合数据到页面 +模型 (Models/Item.cs) +public class Item +{ + public string Name { get; set; } + public double Price { get; set; } +} +控制器 (Controllers/HomeController.cs) +using System.Collections.Generic; +using System.Web.Mvc; +using YourNamespace.Models; + +public class HomeController : Controller +{ + public ActionResult CollectionData() + { + var items = new List + { + new Item { Name = "Apple", Price = 0.99 }, + new Item { Name = "Banana", Price = 0.59 }, + new Item { Name = "Cherry", Price = 2.99 } + }; + return View(items); + } +} +视图 (Views/Home/CollectionData.cshtml) +@model IEnumerable + +

Items

+ + + + + + + + + @foreach (var item in Model) + { + + + + + } + +
NamePrice
@item.Name$@item.Price
\ No newline at end of file diff --git "a/\346\254\247\351\230\263\344\271\276/20241122.png" "b/\346\254\247\351\230\263\344\271\276/20241122.png" new file mode 100644 index 0000000000000000000000000000000000000000..d380da22a1de38de15ad8366effbf570bcc3a446 Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/20241122.png" differ diff --git "a/\346\254\247\351\230\263\344\271\276/20241122\345\237\272\347\241\200\350\203\275\345\212\233.md" "b/\346\254\247\351\230\263\344\271\276/20241122\345\237\272\347\241\200\350\203\275\345\212\233.md" new file mode 100644 index 0000000000000000000000000000000000000000..c36d5db86132518566221d0ed6ba9d587dad3a4b --- /dev/null +++ "b/\346\254\247\351\230\263\344\271\276/20241122\345\237\272\347\241\200\350\203\275\345\212\233.md" @@ -0,0 +1,53 @@ +# 生成一个随机整数,范围[0,100],注意是否包含 +``` +Random random = new Random(); +int randomNumberInclusive = random.Next(0, 101); +Console.WriteLine($"随机数(包含0和100):{randomNumberInclusive}"); +``` +# 生成一个随机整数,范围(0,100],注意是否包含 +``` +Random random = new Random(); +int randomNumberExclusive = random.Next(1, 101); +Console.WriteLine($"随机数(不包含0,包含100):{randomNumberExclusive}"); +``` +# 生成10个随机整数,范围[5,80],注意是否包含 +``` +Random random = new Random(); +List randomNumbers = new List(); +for (int i = 0; i < 10; i++) +{ + int randomNumber = random.Next(5, 81); + randomNumbers.Add(randomNumber); +} +Console.WriteLine("随机数列表(每个在[5, 80]范围内):"); +foreach (var num in randomNumbers) +{ + Console.WriteLine(num); +} +``` +# 定义一个字符串,字符串中有100个中文字符,需要从中随机取1个字符串 +``` +string chineseCharacters = "汉 字 列 表 示 例 子 随 机 生 成 的 一 百 个 汉 字 这 是 一 个 大 型 的 汉 字 集 合 体 系 列 所 有 的 汉 字 都 是 从 中 华 人 民 共 和 国 标 准 字 体 中 抽 取 的 每 一个 汉 字 都 是 独 特 的 不 可 替 换 的 这 些 汉 字 包 括 了 从 简 体 到 繁 体 的 各 种 形 式"; +Random random = new Random(); +char randomChar = chineseCharacters[random.Next(chineseCharacters.Length)]; +Console.WriteLine($"随机选取的字符:{randomChar}"); +``` + +# 定义一个字符串,字符串中有100个中文字符,需要从中随机取5-50个字符,组成新的字符 +``` +string chineseCharacters = "汉 字 列 表 示 例 子 随 机 生 成 的 一 百 个 汉 字 这 是 一 个 大 型 的 汉 字 集 合 体 系 列 所 有 的 汉 字 都 是 从 中 华 人 民 共 和 国 标 准 字 体 中 抽 取 的 每 一个 汉 字 都 是 独 特 的 不 可 替 换 的 这 些 汉 字 包 括 了 从 简 体 到 繁 体 的 各 种 形 式"; +Random random = new Random(); +int length = random.Next(5, 51); // 随机长度在5到50之间 +string randomSubstring = new string(chineseCharacters.OrderBy(x => random.Next()).Take(length).ToArray()); +Console.WriteLine($"随机选取的子字符串:{randomSubstring}"); +``` +# 定义2个字符串,第一个字符串中放百家姓,第二个字符串中放中文字符,要求从第一个字符串随机取得一个姓,再从第二个字符串中随机获得1到2个字符组成新字符串,和第一个字符串取得的姓组成一个姓名 +``` +string chineseCharacters = "汉 字 列 表 示 例 子 随 机 生 成 的 一 百 个 汉 字 这 是 一 个 大 型 的 汉 字 集 合 体 系 列 所 有 的 汉 字 都 是 从 中 华 人 民 共 和 国 标 准 字 体 中 抽 取 的 每 一个 汉 字 都 是 独 特 的 不 可 替 换 的 这 些 汉 字 包 括 了 从 简 体 到 繁 体 的 各 种 形 式"; +Random random = new Random(); +string surname = surnameString[random.Next(surnameString.Length)]; +int nameLength = random.Next(1, 3); // 随机长度在1到2之间 +string randomNamePart = new string(chineseCharacters.OrderBy(x => random.Next()).Take(nameLength).ToArray()); +string fullName = surname + randomNamePart; +Console.WriteLine($"随机生成的姓名:{fullName}"); +``` \ No newline at end of file diff --git "a/\346\254\247\351\230\263\344\271\276/20241122\347\254\224\350\256\260.md" "b/\346\254\247\351\230\263\344\271\276/20241122\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..eb69425f6ed05ce0ccd39615ba3629d64a792c6f --- /dev/null +++ "b/\346\254\247\351\230\263\344\271\276/20241122\347\254\224\350\256\260.md" @@ -0,0 +1,42 @@ +# 制器返回类型 +一般数据类型直接返回 如int、double、string、IEnumerable等数据类型 +IActionResult 一个接口,用于返回HTTP信息状态,如200、401、404等 +视图 +重定向 +ActionResult类型 将一般数据类型和HTTP状态信息混合使用 +特定于格式的操作结果:如JsonResult和ContentResult +POCO(普通旧CLR对象) +在ASP.NET Core MVC中,控制器的Action方法可以返回多种类型的值,以下是一些常见的返回类型: +IActionResult 或 ActionResult: +这是最常用的返回类型,允许你返回不同类型的HTTP响应。例如,可以返回ViewResult、RedirectResult、JsonResult等。 +``` +public IActionResult Index() +{ + return View(); +} +ViewResult: +返回一个视图,通常用于渲染HTML页面。 +``` +``` +public IActionResult Index() +{ + return View(); +} +``` +JsonResult: +返回一个包含JSON数据的HTTP响应。 +``` +public IActionResult GetJson() +{ + var data = new { Name = "张三" }; + return Json(data); +} +``` +ContentResult: +返回一个包含纯文本内容的HTTP响应 +``` +public IActionResult GetText() +{ + return Content("Hello, World!", "text/plain"); +} +``` \ No newline at end of file diff --git "a/\346\254\247\351\230\263\344\271\276/20241124205741.png" "b/\346\254\247\351\230\263\344\271\276/20241124205741.png" new file mode 100644 index 0000000000000000000000000000000000000000..3cdbc5f636ae69d932f97014845fed9c21975853 Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/20241124205741.png" differ diff --git "a/\346\254\247\351\230\263\344\271\276/20241124205753.png" "b/\346\254\247\351\230\263\344\271\276/20241124205753.png" new file mode 100644 index 0000000000000000000000000000000000000000..915a0c5c146d57ea3a578edb63ef1a8d332d8c89 Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/20241124205753.png" differ diff --git "a/\346\254\247\351\230\263\344\271\276/20241126\347\254\224\350\256\260.md" "b/\346\254\247\351\230\263\344\271\276/20241126\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..8aec23640951c84ccc91e6314be650bd596c67d2 --- /dev/null +++ "b/\346\254\247\351\230\263\344\271\276/20241126\347\254\224\350\256\260.md" @@ -0,0 +1,32 @@ +## 视图模型(ViewModel): + +专为视图设计的数据结构,用于在控制器与视图之间传递数据。 + +封装与特定视图相关的数据和逻辑,确保视图代码更易于理解和维护。 + +示例:创建包含文章和评论列表的视图模型来展示文章详情页面 + +## ViewData: + +ControllerBase的属性,类型为ViewDataDictionary,即字典结构。 + +用于在控制器向视图传递数据,以键值对形式存储。 + +数据生命周期在当前请求的视图渲染期间。 + + +控制器(Controller):充当模型和视图之间的中介。它接收来自用户的输入,更新模型的状态,并将更新的数据传递给视图进行显示。 +控制器传参 + +在ASP.NET Core MVC中,控制器通过接收用户请求来处理数据,并将处理结果传递给视图进行展示。控制器和视图之间的数据传递可以通过多种方式实现 +\ No newline at end of file + + +# 正向代理 +pc ————》 由于一些原因不能直接访问web服务器(例如:隐藏ip等原因) ——X——》 web服务器 +pc ——通过找一个——》 代理服务器 ——让代理服务器去访问——》 web服务器 + +# 反向代理 +pc ——X——》 隐藏的web服务器 +web服务器 ————》 设置一个 ————》 代理服务器 +pc端 ————》 访问 ————》 代理服务器(只能通过代理服务器去访问) ————》 访问 ————》 web服务器 \ No newline at end of file diff --git "a/\346\254\247\351\230\263\344\271\276/20241128.1.png" "b/\346\254\247\351\230\263\344\271\276/20241128.1.png" new file mode 100644 index 0000000000000000000000000000000000000000..ff791a72a176d10623fb7a1471a4f8e69bd55b06 Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/20241128.1.png" differ diff --git "a/\346\254\247\351\230\263\344\271\276/20241128.2.png" "b/\346\254\247\351\230\263\344\271\276/20241128.2.png" new file mode 100644 index 0000000000000000000000000000000000000000..866760e5e1b6b5188758b363e3b91c38b6065ee0 Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/20241128.2.png" differ diff --git "a/\346\254\247\351\230\263\344\271\276/20241128.3.png" "b/\346\254\247\351\230\263\344\271\276/20241128.3.png" new file mode 100644 index 0000000000000000000000000000000000000000..c5e8d9b408ecf239d3c85bc535af243ea293e0e4 Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/20241128.3.png" differ diff --git "a/\346\254\247\351\230\263\344\271\276/20241128.4.png" "b/\346\254\247\351\230\263\344\271\276/20241128.4.png" new file mode 100644 index 0000000000000000000000000000000000000000..7077025989cb7344922443bbb9a66d8f0e4cfd4d Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/20241128.4.png" differ diff --git "a/\346\254\247\351\230\263\344\271\276/20241128.png" "b/\346\254\247\351\230\263\344\271\276/20241128.png" new file mode 100644 index 0000000000000000000000000000000000000000..fa31dd7caf8287f4c07c3f57e0e19ad6d6ff1fc8 Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/20241128.png" differ diff --git "a/\346\254\247\351\230\263\344\271\276/20241128\347\254\224\350\256\260.md" "b/\346\254\247\351\230\263\344\271\276/20241128\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..3d1660825eb5607021af25071eb40e3a52f920e0 --- /dev/null +++ "b/\346\254\247\351\230\263\344\271\276/20241128\347\254\224\350\256\260.md" @@ -0,0 +1,9 @@ +# 视图定义属性 +定义: @{属性(键值对)} 调用: @键 + +接受view +@model 命名空间,类名 #model. 键 + +# 定位点 + + \ No newline at end of file diff --git "a/\346\254\247\351\230\263\344\271\276/20241205linq\347\273\203\344\271\240.md" "b/\346\254\247\351\230\263\344\271\276/20241205linq\347\273\203\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..443e282aa1a60bd38ce686190d00d6051c58a552 --- /dev/null +++ "b/\346\254\247\351\230\263\344\271\276/20241205linq\347\273\203\344\271\240.md" @@ -0,0 +1,284 @@ +# 基础练习 +1.查询特定元素 找出数组中等于5的元素。 + +int[] numbers = { 1, 2, 3, 4, 5, 6 }; +```c# + var result = numbers.Where(n => n == 5).ToArray(); + + Console.WriteLine("等于5的元素:"); + foreach (var number in result) + { + Console.WriteLine(number); + } +``` +2.查询特定范围的元素 找出数组中在2到8之间的元素。 + +int[] numbers = { 1, 2, 3, 4, 5, 6 }; +```c# +var result = numbers.Where(n => n >= 2 && n <= 8).ToArray(); + +``` +3.查询并转换元素 将数组中的每个数字乘以2。 + +int[] numbers = { 1, 2, 3, 4, 5, 6 }; +```c# + var result = numbers.Select(n => n * 2).ToArray(); +``` +4.查询特定属性的对象 找出所有名字以"王"开头的学生。 + +List students = new List +{ + new Student {Id=1, Name = "王有才", Age = 21 }, + new Student {Id=2, Name = "王中王", Age = 22 }, + new Student {Id=3, Name = "张语嫣", Age = 23 }, + new Student {Id=4, Name = "詹宇航", Age = 35 }, + new Student {Id=5, Name = "郑雨良", Age = 26 }, +}; +```c# +var result = students.Where(s => s.Name.StartsWith("王")).ToList(); +``` +5.查询并排序 找出所有年龄大于20岁的学生,并按年龄降序排列。 + + List students = new List +{ + new Student {Id=1, Name = "王有才", Age = 21 }, + new Student {Id=2, Name = "罗婷", Age = 21 }, + new Student {Id=3, Name = "王中王", Age = 22 }, + new Student {Id=4, Name = "李子柒", Age = 22 }, + new Student {Id=5, Name = "张语嫣", Age = 23 }, + new Student {Id=6, Name = "詹宇航", Age = 35 }, + new Student {Id=7, Name = "郑雨良", Age = 26 }, + new Student {Id=8, Name = "欧文", Age = 26 }, +}; +```c# + var result = students.Where(s => s.Age > 20) + .OrderByDescending(s => s.Age) + .ToList(); +``` +6.查询并去重 找出数组中所有不重复的数字。 + +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +```c# + var distinctNumbers = numbers.Distinct().ToArray(); +``` +7.查询第一个元素 找出数组中第一个大于3的元素。 + +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +```c# + var firstGreaterThanThree = numbers.FirstOrDefault(n => n > 3); +``` +8.查询最后一个元素 找出数组中最后一个小于7的元素。 + +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +```c# + var lastLessThanSeven = numbers.LastOrDefault(n => n < 7); +``` +9.查询元素是否存在 检查数组中是否存在大于10的元素。 + +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +```c# + bool existsGreaterThanTen = numbers.Any(n => n > 10); +``` +10.查询元素的计数 计算数组中大于5的元素数量。 + +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +```c# +int countGreaterThanFive = numbers.Count(n => n > 5); +``` +11.查询元素的总和 计算数组中所有元素的总和。 + +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +```c# + int sum = numbers.Sum(); +``` +12.查询元素的最大值 找出数组中的最大值。 + +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +```c# +int maxValue = numbers.Max(); +``` +13.查询元素的最小值 找出数组中的最小值。 + +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +```c# +int minValue = numbers.Min(); +``` +14.查询元素的平均值 计算数组中所有元素的平均值。 + +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +```c# + double average = numbers.Average(); +``` +15.查询特定条件的元素 找出数组中能被3整除的元素。 + +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +```c# + var divisibleByThree = numbers.Where(n => n % 3 == 0).ToArray(); +``` +# 中级练习 +16.查询并选择特定属性 找出所有学生的姓名和年龄。 + +List students = new List +{ + new Student {Id=1, Name = "王有才", Age = 21 }, + new Student {Id=2, Name = "罗婷", Age = 21 }, + new Student {Id=3, Name = "王中王", Age = 22 }, + new Student {Id=4, Name = "李子柒", Age = 22 }, + new Student {Id=5, Name = "张语嫣", Age = 23 }, + new Student {Id=6, Name = "詹宇航", Age = 35 }, + new Student {Id=7, Name = "郑雨良", Age = 26 }, + new Student {Id=8, Name = "欧文", Age = 26 }, +}; +```c# +var result = students.Select(s => new { s.Name, s.Age }).ToList(); + foreach (var student in result) + { + Console.WriteLine($"姓名: {student.Name}, 年龄: {student.Age}"); + } +``` +17.查询并分组 按年龄分组学生,并计算每个年龄组的学生数量。 + +List students = new List +{ + new Student {Id=1, Name = "王有才", Age = 21 }, + new Student {Id=2, Name = "罗婷", Age = 21 }, + new Student {Id=3, Name = "王中王", Age = 22 }, + new Student {Id=4, Name = "李子柒", Age = 22 }, + new Student {Id=5, Name = "张语嫣", Age = 23 }, + new Student {Id=6, Name = "詹宇航", Age = 35 }, + new Student {Id=7, Name = "郑雨良", Age = 26 }, + new Student {Id=8, Name = "欧文", Age = 26 }, +}; +```c# +var groupedStudents = students.GroupBy(s => s.Age) + .Select(g => new { Age = g.Key, Count = g.Count() }) + .ToList(); +``` +18.查询并联结 联结学生和课程表,找出每个学生的所有课程。 + +List students = new List +{ + new Student {Id=1, Name = "王有才", Age = 21 }, + new Student {Id=2, Name = "罗婷", Age = 21 }, + new Student {Id=3, Name = "王中王", Age = 22 }, + new Student {Id=4, Name = "李子柒", Age = 22 }, + new Student {Id=5, Name = "张语嫣", Age = 23 }, + new Student {Id=6, Name = "詹宇航", Age = 35 }, + new Student {Id=7, Name = "郑雨良", Age = 26 }, + new Student {Id=8, Name = "欧文", Age = 26 }, +}; +List courses=new List +{ + new Course{StudentId=1,CourseName="英语"}, + new Course{StudentId=1,CourseName="数学"}, + new Course{StudentId=2,CourseName="语文"}, + new Course{StudentId=3,CourseName="物理"}, + new Course{StudentId=4,CourseName="化学"}, + new Course{StudentId=4,CourseName="生物"}, + new Course{StudentId=4,CourseName="语文"}, +}; +```c# +// 联结学生和课程 + var studentCourses = from student in students + join course in courses on student.Id equals course.StudentId into courseGroup + select new + { + StudentName = student.Name, + Courses = courseGroup.Select(c => c.CourseName).ToList() + }; + + // 输出结果 + foreach (var sc in studentCourses) + { + Console.WriteLine($"学生: {sc.StudentName}, 课程: {string.Join(", ", sc.Courses)}"); + } +``` +19.查询并反转 反转数组中的元素顺序。 + +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +```c# +var reversedNumbers = numbers.Reverse().ToArray(); +``` +20.查询并填充 找出数组中第一个大于2的元素,并用它填充后面的所有位置。 + +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +```c# +// 找到第一个大于2的元素 + int? firstGreaterThanTwo = numbers.FirstOrDefault(n => n > 2); + + // 用找到的元素填充后面的所有位置 + for (int i = 0; i < numbers.Length; i++) + { + if (firstGreaterThanTwo.HasValue && i >= Array.IndexOf(numbers, firstGreaterThanTwo)) + { + numbers[i] = firstGreaterThanTwo.Value; + } + } + + Console.WriteLine("填充后的数组:"); + Console.WriteLine(string.Join(", ", numbers)); +``` +21.查询并排除 从数组中排除所有小于5的元素。 + +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +```c# + var filteredNumbers = numbers.Where(n => n >= 5).ToArray(); + + Console.WriteLine("排除小于5的元素后的数组:"); + Console.WriteLine(string.Join(", ", filteredNumbers)); +``` +22.查询并填充默认值 如果数组中存在null值,用默认值0替换。 + +int?[] nullableNumbers = { 1, null, 3, null, 5 }; +```c# + var filledNumbers = nullableNumbers.Select(n => n ?? 0).ToArray(); +``` +23.查询并转换类型 将字符串数组转换为整数数组。 + +string[] stringNumbers = { "1", "2", "3", "4" }; +```c# + int[] intNumbers = stringNumbers.Select(int.Parse).ToArray(); +``` +24.查询并使用OfType过滤 从对象数组中过滤出字符串类型的元素。 + +object[] objects = { "String", 123, "Another String", 456 }; +var result = objects.OfType().ToList(); +```c# +var result = objects.OfType().ToList(); +``` +# 高级练习 +25.查询并使用Zip合并 合并两个数组,并创建一个包含元素对的新数组。 + +int[] numbers1 = { 1, 2, 3 }; +int[] numbers2 = { 4, 5, 6 }; +```c# +// 使用 Zip 方法合并两个数组 + var zipped = numbers1.Zip(numbers2, (n1, n2) => new { n1, n2 }).ToArray(); + + // 输出合并后的数组 + Console.WriteLine("合并后的数组:"); + foreach (var pair in zipped) + { + Console.WriteLine($"({pair.n1}, {pair.n2})"); + } +``` +26.查询并使用Range生成 生成一个包含1到10的整数数组。 +```c# +int[] numbers = Enumerable.Range(1,10).ToArray(); +``` +27.查询并使用Repeat重复 重复一个元素多次,创建一个新数组。 +```c# +int[] numbers = Enumerable.Repeat(1,10).ToArray(); +``` +28.查询并使用Take限制数量 从数组中取出前5个元素。 + +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; +```c# +var takenNumbers = numbers.Take(5).ToArray(); +``` +29.查询并使用Skip跳过元素 跳过数组中的前3个元素,然后取出剩余的元素。 + +int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 } +```c# + var skippedNumbers = numbers.Skip(3).ToArray(); +``` \ No newline at end of file diff --git "a/\346\254\247\351\230\263\344\271\276/20241205\347\254\224\350\256\260.md" "b/\346\254\247\351\230\263\344\271\276/20241205\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..1517f288bc09fe8a4c8b80a5172380421a075ef7 --- /dev/null +++ "b/\346\254\247\351\230\263\344\271\276/20241205\347\254\224\350\256\260.md" @@ -0,0 +1,14 @@ +## 表单 +基本表单结构: +``` +
+ + +``` +控制器: +在该方法名前面加 `[HttpPost]` +该方法参数为 `(类型 变量名)` + +删除写一个新的方法 +按钮`` +进行删除然后返回首页 \ No newline at end of file diff --git "a/\346\254\247\351\230\263\344\271\276/3.PNG" "b/\346\254\247\351\230\263\344\271\276/3.PNG" new file mode 100644 index 0000000000000000000000000000000000000000..de48dade52758b3b572b59c82b11f76e9f74fdbc Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/3.PNG" differ diff --git "a/\346\254\247\351\230\263\344\271\276/4.PNG" "b/\346\254\247\351\230\263\344\271\276/4.PNG" new file mode 100644 index 0000000000000000000000000000000000000000..ef340072ab427c5db0fa66889cbdbc40b077b390 Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/4.PNG" differ diff --git "a/\346\254\247\351\230\263\344\271\276/5.PNG" "b/\346\254\247\351\230\263\344\271\276/5.PNG" new file mode 100644 index 0000000000000000000000000000000000000000..1d526fcd7b7ad7737919578ac94a6383553b5151 Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/5.PNG" differ diff --git "a/\346\254\247\351\230\263\344\271\276/6.PNG" "b/\346\254\247\351\230\263\344\271\276/6.PNG" new file mode 100644 index 0000000000000000000000000000000000000000..2fc1f875ccc65106b2b2df94b60283f0aadc6c29 Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/6.PNG" differ diff --git "a/\346\254\247\351\230\263\344\271\276/linux.gif" "b/\346\254\247\351\230\263\344\271\276/linux.gif" new file mode 100644 index 0000000000000000000000000000000000000000..db48a3b4f540a22f447b3fed9e061ba9cd608e1d Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/linux.gif" differ diff --git "a/\346\254\247\351\230\263\344\271\276/linux1.gif" "b/\346\254\247\351\230\263\344\271\276/linux1.gif" new file mode 100644 index 0000000000000000000000000000000000000000..2cc358e1b60a7b30d77f97884b07a58e67fa9410 Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/linux1.gif" differ diff --git "a/\346\254\247\351\230\263\344\271\276/\344\275\234\344\270\232\346\225\210\346\236\234\345\233\276.png" "b/\346\254\247\351\230\263\344\271\276/\344\275\234\344\270\232\346\225\210\346\236\234\345\233\276.png" new file mode 100644 index 0000000000000000000000000000000000000000..f2bab92e086336e325311a8cd0213bc65e6d19ac Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/\344\275\234\344\270\232\346\225\210\346\236\234\345\233\276.png" differ diff --git "a/\346\254\247\351\230\263\344\271\276/\345\210\240\351\231\244.PNG" "b/\346\254\247\351\230\263\344\271\276/\345\210\240\351\231\244.PNG" new file mode 100644 index 0000000000000000000000000000000000000000..aa6e4f9568774019f28442ac73142b433a87ddcf Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/\345\210\240\351\231\244.PNG" differ diff --git "a/\346\254\247\351\230\263\344\271\276/\346\226\260\345\242\236.PNG" "b/\346\254\247\351\230\263\344\271\276/\346\226\260\345\242\236.PNG" new file mode 100644 index 0000000000000000000000000000000000000000..22ed2f173c666dbc51984ab75d6b1c729b741eb1 Binary files /dev/null and "b/\346\254\247\351\230\263\344\271\276/\346\226\260\345\242\236.PNG" differ