From df53ede839956312201ee5d1d6fb662394f04597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=A9=89=E5=A9=B7?= <2465898445@qq.com> Date: Sun, 8 Dec 2024 13:26:32 +0800 Subject: [PATCH] tijiao --- ...36\345\210\240\346\224\271\346\237\245.md" | 343 ++++++++++++ ...04\345\242\236\345\222\214\346\224\271.md" | 240 ++++++++ ...44\345\222\214\346\237\245\350\257\242.md" | 527 ++++++++++++++++++ 3 files changed, 1110 insertions(+) create mode 100644 "\347\216\213\345\251\211\345\251\267/20241202-mvc\347\232\204\345\242\236\345\210\240\346\224\271\346\237\245.md" create mode 100644 "\347\216\213\345\251\211\345\251\267/20241204-\345\242\236\345\210\240\346\224\271\346\237\245\347\232\204\345\242\236\345\222\214\346\224\271.md" create mode 100644 "\347\216\213\345\251\211\345\251\267/20241205-\345\242\236\345\210\240\346\224\271\346\237\245\347\232\204\345\210\240\351\231\244\345\222\214\346\237\245\350\257\242.md" diff --git "a/\347\216\213\345\251\211\345\251\267/20241202-mvc\347\232\204\345\242\236\345\210\240\346\224\271\346\237\245.md" "b/\347\216\213\345\251\211\345\251\267/20241202-mvc\347\232\204\345\242\236\345\210\240\346\224\271\346\237\245.md" new file mode 100644 index 0000000..8e00172 --- /dev/null +++ "b/\347\216\213\345\251\211\345\251\267/20241202-mvc\347\232\204\345\242\236\345\210\240\346\224\271\346\237\245.md" @@ -0,0 +1,343 @@ +# 在VSCore中的页面的增删改查(以Blog项目为例) + +## 1.创建项目(无解决方案)复杂项目才需要 +```cs +dotnet new mvc -o Blog +``` + +## 2.控制器 `BlogsController.cs` +- 控制器(`Controller`)名字和视图(`View`)中的文件名要一模一样 +```cs +using Microsoft.AspNetCore.Mvc; +//Blog项目名 Models中有新建文件需要引用 +using Blog.Models; +//Blog项目名 +namespace Blog.Controllers; +//BlogsController跟控制器名字取的一样 +public class BlogsController : Controller +{ + // 返回视图 用于整个页面 + public IActionResult Index() + { + return View(Db.Blogs); + } + // 增加页面 + public IActionResult Increase() + { + return View(); + } + // 编辑页面 + public IActionResult Redact() + { + return View(); + } + // 删除页面 + public IActionResult Delete() + { + return View(); + } +} +``` + +## 3. `_ViewStart.cshtml`中的默认模板页面可改为空(选择) +```cs +@{ + Layout = null; +} +``` + +## 4. 在View中 +- 控制器(`Controller`)名字和视图(`View`)中的文件名要一模一样 +- 1. 创建文件夹`Blogs` +- 2. 创建文件`Index.cshtml` + + +## 5. 在Properties中(选择) +- launchSettings.json 中端口可改为5000 + +## 6. 在4.中的Index.cshtml中写需要的内容页面 + +## 7. 在wwwroot中写css文件(如需css文件的话) +- css文件名为`base.css` +- link在Index.cshtml页面中书写 +```html + +``` + +## 8. 在Models中创建Blogs.cs 模型 +- 字段名 +- Blogs.cs中 +```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!; + + +} +``` + +## 9. 在Models中创Db.cs(模拟数据库创建) +- 静态字段 +- Db.cs中 +```cs +namespace Blog.Models; +public static class Db +{ + // 集合 + public static List Blogs{get;set;} + + // 构造函数 + static Db() + { + Blogs=[]; + for (var i = 1; i <=10; i++) + { + var tmp = new Blogs + { + Id=i, + Title=$"永远是朋友{i}", + Content=$"假日风情{i}", + Author="哈哈" + }; + Blogs.Add(tmp); + } + } +} +``` + +## 10. 在Index.cshtml中 +- 增删改查需要跳转的页面就改换位a标签 + - input(button) --》 改换成a标签 +- **asp-action可以跳转到书写的页面** +- `Increase` 是在Views下的Blogs中创建的`Increase.cshtml` +```html + 增加 +``` +## 11. 在Models中写的Db.cs + - 记得在控制器`BlogsController.cs`中返回视图 + ```cs + public IActionResult Index() + { + return View(Db.Blogs); + } +``` + + +# 完整版 +## 1. 在Controllers需要写的文件 +- BlogsController.cs中 +```cs +using Microsoft.AspNetCore.Mvc; +using Blog.Models; +namespace Blog.Controllers; + +public class BlogsController : Controller +{ + public IActionResult Index() + { + return View(Db.Blogs); + } + public IActionResult Increase() + { + return View(); + } + + public IActionResult Redact() + { + return View(); + } + public IActionResult Delete() + { + return View(); + } +} +``` + +## 2. 在Models中需要写的文件 +- 在Blogs.cs文件中 +```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文件中 +```cs + +namespace Blog.Models; +public static class Db +{ + public static List Blogs{get;set;} + + static Db() + { + Blogs=[]; + for (var i = 1; i <=10; i++) + { + var tmp = new Blogs + { + Id=i, + Title=$"永远的友谊{i}", + Content=$"开心每一天{i}", + Author="哈哈" + }; + Blogs.Add(tmp); + } + } +} +``` + +## 3. Views +- 在Views下创建一个文件夹 Blogs +- 在Blogs中创建Index.cs + ```cs + +@model List + 增加 + + + + + + + + + @foreach(var item in @Model) + { + + + + + + + + + } +
Id标题内容作者操作
@item.Id@item.Title@item.Content@item.Author + 编辑 + 删除 + +
+``` +- 在Increase.cs文件中 +```cs +

新增

+ + + + + + + + + + + + + + + + + + + + + + + + + +
标题
内容
作者
+``` + +- 在Redact.cshtml文件中 +```cs +

修改

+ + + + + + + + + + + + + + + + + + + + + + + + + +
标题
内容
作者
+``` + +## 4. 在wwwroot中的css +- 创建base.cs文件 +```css +table, +tr, +th, +td{ + border: 1px solid; + border-collapse: collapse; +} + +th{ + width: 100px; + height: 40px; + background-color: paleturquoise; +} +tr{ + width: 100px; + height: 30px; +} +a{ + + display: inline-block; + width: 40px; + height: 30px; + line-height: 30px; + text-decoration: none; + background-color: rgb(127, 228, 228); + color: papayawhip; + border: 1px solid; + border-radius: 10px; + text-align: center; +} +a:nth-child(2){ + background-color: plum; + color: papayawhip; +} +``` + + +### 完成完整版的以上步骤后 +- 1. 可以在进入到Blog文件中运行 + - 热重载 + - `dotnet watch` +- 2. 打开Index页面 + - 指的是在Blogs文件夹中的Index.cshtml文件 + - Index改成Blogs文件下的其他名字就会跳转到对应的页面 + - `http://localhost:5212/blogs/index` \ No newline at end of file diff --git "a/\347\216\213\345\251\211\345\251\267/20241204-\345\242\236\345\210\240\346\224\271\346\237\245\347\232\204\345\242\236\345\222\214\346\224\271.md" "b/\347\216\213\345\251\211\345\251\267/20241204-\345\242\236\345\210\240\346\224\271\346\237\245\347\232\204\345\242\236\345\222\214\346\224\271.md" new file mode 100644 index 0000000..3d821cf --- /dev/null +++ "b/\347\216\213\345\251\211\345\251\267/20241204-\345\242\236\345\210\240\346\224\271\346\237\245\347\232\204\345\242\236\345\222\214\346\224\271.md" @@ -0,0 +1,240 @@ + +# Linq集成查询(关联Lambda) +1. First FirstOrDefault 找到第一个符合条件的元素 + - First(x =>x.Id == id) 返回第一个Id等于id的元素,如果都没有符合的,报错 + - FirstOrDefault(x =>x.Id == id) 返回第一个Id等于id的元素,如果都没有符合的,返回Null + +2. Single SingleOrDefault + - Single() 返回第一个Id等于id的元素,如果都没有符合的,报错 + - SingleOrDefault() 返回第一个Id等于id的元素,如果都没有符合的,返回Null + +3. Where + - Where(x=>x.Score>=80 && x.Sex==1) 查找所有成绩大于等于80,并且性别为1的所有元素 + 4.Select + - Select(x=>new{x.Id,x.Score}) 以新的{x.Id,x.Score}对象形式,返回新的集合 +# 如何将增删改查的增改添加数据传输到页面 + +## 1. 在新增页面Increase.cshtml中修改为 +```c# +@model Blog.Models.Blogs; +

新增

+
+
+
+
+ +
+``` +## 2. 在BlogsController.cs中添加 +```cs + [HttpPost] + public Blogs Increase(Blogs input) + { + return input; + } +``` + +-就可以添加新增到页面。但需要验证数据库是否成功,成功跳转到列表页吗,验证不通过,仍显示新增页面,并显示 +- 所以单单这样,验证是不通过的,并且显示 +```c# +{ + "id": 0, + "title": "你好", + "content": "星期三", + "author": "哈哈" +} +``` + + +# 完整版 (需要配合上一篇,mvc的增删改查一起) +- BlogsController.cs +```c# +using Microsoft.AspNetCore.Mvc; +using Blog.Models; +namespace Blog.Controllers; + +public class BlogsController : Controller +{ + public IActionResult Index() + { + return View(Db.Blogs); + } + /// + /// 创建-展示新增页面 + /// + /// + public IActionResult Increase() + { + return View(); + } + /// + /// 创建-保存表单结果的Action + /// + /// + /// + [HttpPost] + public IActionResult Increase(Blogs input) + { + // return input; + + // 1.验证表单数据是否可以传入 + // 2.拿到传入的数据后,一般做验证,数据验证,如必填,手机号,长度,名称是否唯一 + // 3.如果符合验证规则,则保存到数据库,否则提示验证不通过 + // 4.如果保存数据库成功,则跳转列表页,如果验证不成功,那就仍然显示新增页面 + // var maxId=Db.Blogs.First(); + // var maxId=Db.Blogs.FirstOrDefault(); + // var maxId=Db.Blogs.Single(x=>x.Id>0); + // var maxId=Db.Blogs.SingleOrDefault(x=>x.Id>0); + // double.Blogs.Add(inout) + + + // 先通过select 拿到集合中的所有id,放在一个新的集合中返回,然后对这个返回的新的集合应用Max方法,找到其中最大值 + // var blogs = Db.Blogs.Where(x => x.Title.Equals(input.Title)); + // if (blogs.Count() > 0) + // { + // return View("create"); + // } + + var maxId=Db.Blogs.Select(x =>x.Id).Max(); + input.Id=maxId +1; + Db.Blogs.Add(input); + return RedirectToAction("Index"); + } + // 新 + // 获得Id + public IActionResult Redact(int id) + { + // 根据id找到对应的blogs,有可能为空 + var blog = Db.Blogs.FirstOrDefault(x =>x.Id == id); + return View(blog); + } + // 新 + [HttpPost] + public IActionResult Redact(Blogs input) + { + // 根据id找到对应的blogs,有可能为空 + var blog = Db.Blogs.FirstOrDefault(x => x.Id == input.Id); + if (blog != null) + { + blog.Title = input.Title; + blog.Content = input.Content; + blog.Author = input.Author; + + } + return RedirectToAction("Index"); + } + public IActionResult Delete() + { + return View(); + } +} +``` +- Index.cshtml页面 +```cs + +@model List + 增加 + + + + + + + + + @foreach(var item in @Model) + { + + + + + + + + + } +
Id标题内容作者操作
@item.Id@item.Title@item.Content@item.Author + 编辑 + 删除 + +
+``` +- Increase.cshtml页面 +```html +@model Blog.Models.Blogs; +

新增

+
+
+
+
+ +
+ +@*注释部分 + + + + + + + + + + + + + + + + + + + + + + + + + +
标题
内容
作者
*@ +``` +- Redact.cshtml页面 +```html +@model Blog.Models.Blogs; +

修改

+
+ +
+
+
+
+ +
+@* + + + + + + + + + + + + + + + + + + + + + + + + +
标题
内容
作者
*@ + +``` \ No newline at end of file diff --git "a/\347\216\213\345\251\211\345\251\267/20241205-\345\242\236\345\210\240\346\224\271\346\237\245\347\232\204\345\210\240\351\231\244\345\222\214\346\237\245\350\257\242.md" "b/\347\216\213\345\251\211\345\251\267/20241205-\345\242\236\345\210\240\346\224\271\346\237\245\347\232\204\345\210\240\351\231\244\345\222\214\346\237\245\350\257\242.md" new file mode 100644 index 0000000..7398807 --- /dev/null +++ "b/\347\216\213\345\251\211\345\251\267/20241205-\345\242\236\345\210\240\346\224\271\346\237\245\347\232\204\345\210\240\351\231\244\345\222\214\346\237\245\350\257\242.md" @@ -0,0 +1,527 @@ +# 增删改查的删除和查询 + +# 删除 +## 1. 在Redact.cshtml中 +```html + @* 让Id隐藏起来在控制台,不让用户修改 *@ +
+``` + +## 2. 在BlogsController.cs中 +```cs + // 删除的修改 + public IActionResult Delete(int id) + { + // 根据id找到对应的blogs,有可能为空 + var blog = Db.Blogs.FirstOrDefault(x => x.Id == id); + + return View(blog); + } +``` + +## 3. 在Delete.cshtml中 +```html +@model Blog.Models.Blogs; + +

你真的要删除以下的信息吗

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
标题@Model.Title
内容@Model.Content
作者@Model.Author
+ @* *@ + 删除 + + @* *@ + @* 换成可以跳转页面的模式,取消就跳到首页 *@ + 取消 +
+``` +## 4. 在BlogsController.cs中新增 +```cs +// 新增 + public IActionResult DeleteConfirm(int id) + { + // 1.根据id查找数据库的数据 + // 2.查找到后,就删除 + // 3.找不到就返回找不到博客的提示信息 + // 4.删除成功后,返回列表页 + + var blog = Db.Blogs.FirstOrDefault(x=>x.Id=id); + // 有找到,则尝试删除 + if(blog!= null){ + Db.Blogs.Remove(blog); + // 删除后返回首页 + return RedirecToAction("Index") + } + // 没有找到 + return NotFound(); + } +``` + +# 查询 +## 1.首先在Index.cshtml页面中添加查询的输入框 +```html + + +``` + +## 2.在BlogsController.cs中修改Index +```c# + public IActionResult Index(string keyword) + { + // 两种情况 + // 一种查找,有keyword, + // 另一种没有查找,没有keyword + keyword = keyword.Trim(); + if(string.IsNullOrEmpty(keyword)){ + return View(Db.Blogs); + } + var list Db.Blogs.Where(x=>x.Title.Contains(keyword) + || x.Content.Contains(keyword) || x.Author.Contains(keyword)).ToList(); + return View(list); + + } +``` + +## 3.将Index.cshtml中修改器为 +```html + @* + *@ +
+

+ + +

+
+``` + + +## 1-3题 +我们将在ASP.NET Core MVC项目中执行三个不同的LINQ查询操作,并将结果通过视图模型返回到视图。以下是详细步骤: + +### 步骤 1: 创建ASP.NET Core MVC项目 + +1. 打开VS Core。 +2. 创建一个新的项目。 +3. 选择“ASP.NET Core Web 应用”模板。 +4. 选择MVC作为项目模板。 +5. 点击“创建”并等待项目创建完成。 + +### 步骤 2: 定义ViewModel + +在模型文件夹(通常是`Models`)中创建三个视图模型类。 + +```csharp +// EqualToFiveViewModel.cs +namespace YourNamespace.Models +{ + public class EqualToFiveViewModel + { + public IEnumerable Numbers { get; set; } + } +} + +// InRangeViewModel.cs +namespace YourNamespace.Models +{ + public class InRangeViewModel + { + public IEnumerable Numbers { get; set; } + } +} + +// MultiplyByTwoViewModel.cs +namespace YourNamespace.Models +{ + public class MultiplyByTwoViewModel + { + public IEnumerable Numbers { get; set; } + } +} +``` + +### 步骤 3: 编写LINQ查询代码 + +在控制器中,添加三个Action方法来执行LINQ查询并返回结果。 + +```csharp +using Microsoft.AspNetCore.Mvc; +using System; +using System.Linq; +using YourNamespace.Models; // 确保引用了模型的命名空间 + +namespace YourNamespace.Controllers +{ + public class QueryController : Controller + { + // GET: Query/EqualToFive + public IActionResult EqualToFive() + { + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var equalToFives = numbers.Where(n => n == 5).ToList(); + var model = new EqualToFiveViewModel { Numbers = equalToFives }; + return View(model); + } + + // GET: Query/InRange + public IActionResult InRange() + { + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var inRange = numbers.Where(n => n >= 2 && n <= 8).ToList(); + var model = new InRangeViewModel { Numbers = inRange }; + return View(model); + } + + // GET: Query/MultiplyByTwo + public IActionResult MultiplyByTwo() + { + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var multipliedByTwo = numbers.Select(n => n * 2).ToList(); + var model = new MultiplyByTwoViewModel { Numbers = multipliedByTwo }; + return View(model); + } + } +} +``` + +### 步骤 4: 创建视图 + +对于每个Action方法,在`Views/Query`目录下创建相应的视图文件,并添加代码以显示结果。 + +```html + +@model YourNamespace.Models.EqualToFiveViewModel +
    + @foreach (var number in Model.Numbers) + { +
  • @number
  • + } +
+``` + +```html + +@model YourNamespace.Models.InRangeViewModel +
    + @foreach (var number in Model.Numbers) + { +
  • @number
  • + } +
+``` + +```html + +@model YourNamespace.Models.MultiplyByTwoViewModel +
    + @foreach (var number in Model.Numbers) + { +
  • @number
  • + } +
+``` +## 6-15题融合成一个文件里 +为了完成这些查询并将结果返回到视图中,我们将创建一个ASP.NET Core MVC项目,并为每个查询创建一个单独的视图模型。以下是详细步骤: + +### 步骤 1: 创建ASP.NET Core MVC项目 + +1. 打开VS Core。 +2. 创建一个新的项目。 +3. 选择“ASP.NET Core Web 应用”模板。 +4. 选择MVC作为项目模板。 +5. 点击“创建”并等待项目创建完成。 + +### 步骤 2: 定义多个ViewModel + +在模型文件夹(通常是`Models`)中为每个查询创建一个新的视图模型类。 + +```csharp +// UniqueNumbersViewModel.cs +namespace YourNamespace.Models +{ + public class UniqueNumbersViewModel + { + public IEnumerable UniqueNumbers { get; set; } + } +} + +// FirstGreaterThanThreeViewModel.cs +namespace YourNamespace.Models +{ + public class FirstGreaterThanThreeViewModel + { + public int? FirstGreaterThanThree { get; set; } + } +} + +// LastLessThanSevenViewModel.cs +namespace YourNamespace.Models +{ + public class LastLessThanSevenViewModel + { + public int? LastLessThanSeven { get; set; } + } +} + +// ExistsGreaterThanTenViewModel.cs +namespace YourNamespace.Models +{ + public class ExistsGreaterThanTenViewModel + { + public bool Exists { get; set; } + } +} + +// CountGreaterThanFiveViewModel.cs +namespace YourNamespace.Models +{ + public class CountGreaterThanFiveViewModel + { + public int Count { get; set; } + } +} + +// SumAllElementsViewModel.cs +namespace YourNamespace.Models +{ + public class SumAllElementsViewModel + { + public int Sum { get; set; } + } +} + +// MaxValueViewModel.cs +namespace YourNamespace.Models +{ + public class MaxValueViewModel + { + public int MaxValue { get; set; } + } +} + +// MinValueViewModel.cs +namespace YourNamespace.Models +{ + public class MinValueViewModel + { + public int MinValue { get; set; } + } +} + +// AverageValueViewModel.cs +namespace YourNamespace.Models +{ + public class AverageValueViewModel + { + public double AverageValue { get; set; } + } +} + +// DivisibleByThreeViewModel.cs +namespace YourNamespace.Models +{ + public class DivisibleByThreeViewModel + { + public IEnumerable DivisibleByThree { get; set; } + } +} +``` + +### 步骤 3: 编写LINQ查询代码 + +在控制器中,你可以添加一个Action方法来执行LINQ查询并返回结果。 + +```csharp +using Microsoft.AspNetCore.Mvc; +using System; +using System.Linq; +using YourNamespace.Models; // 确保引用了模型的命名空间 + +namespace YourNamespace.Controllers +{ + public class NumberQueryController : Controller + { + // GET: NumberQuery/UniqueNumbers + public IActionResult UniqueNumbers() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var model = new UniqueNumbersViewModel { UniqueNumbers = numbers.Distinct() }; + return View(model); + } + + // GET: NumberQuery/FirstGreaterThanThree + public IActionResult FirstGreaterThanThree() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var model = new FirstGreaterThanThreeViewModel { FirstGreaterThanThree = numbers.FirstOrDefault(n => n > 3) }; + return View(model); + } + + // GET: NumberQuery/LastLessThanSeven + public IActionResult LastLessThanSeven() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var model = new LastLessThanSevenViewModel { LastLessThanSeven = numbers.LastOrDefault(n => n < 7) }; + return View(model); + } + + // GET: NumberQuery/ExistsGreaterThanTen + public IActionResult ExistsGreaterThanTen() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var model = new ExistsGreaterThanTenViewModel { Exists = numbers.Any(n => n > 10) }; + return View(model); + } + + // GET: NumberQuery/CountGreaterThanFive + public IActionResult CountGreaterThanFive() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var model = new CountGreaterThanFiveViewModel { Count = numbers.Count(n => n > 5) }; + return View(model); + } + + // GET: NumberQuery/SumAllElements + public IActionResult SumAllElements() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var model = new SumAllElementsViewModel { Sum = numbers.Sum() }; + return View(model); + } + + // GET: NumberQuery/MaxValue + public IActionResult MaxValue() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var model = new MaxValueViewModel { MaxValue = numbers.Max() }; + return View(model); + } + + // GET: NumberQuery/MinValue + public IActionResult MinValue() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var model = new MinValueViewModel { MinValue = numbers.Min() }; + return View(model); + } + + // GET: NumberQuery/AverageValue + public IActionResult AverageValue() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var model = new AverageValueViewModel { AverageValue = numbers.Average() }; + return View(model); + } + + // GET: NumberQuery/DivisibleByThree + public IActionResult DivisibleByThree() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var model = new DivisibleByThreeViewModel { DivisibleByThree = numbers.Where(n => n % 3 == 0) }; + return View(model); + } + } +} +``` + +### 步骤 4: 创建视图 + +对于每个Action方法,在`Views/NumberQuery`目录下创建相应的视图文件,并添加代码以显示结果。 + +```html + +@model YourNamespace.Models.UniqueNumbersViewModel +
    + @foreach (var number in Model.UniqueNumbers) + { +
  • @number
  • + } +
+``` + +```html + +@model YourNamespace.Models.FirstGreaterThanThreeViewModel +

First number greater than 3: @Model.FirstGreaterThanThree

+``` + +```html + +@model YourNamespace.Models.LastLessThanSevenViewModel +

Last number less than 7: @Model.LastLessThanSeven

+``` + +```html + +@model YourNamespace.Models.ExistsGreaterThanTenViewModel +

Exists number greater than 10: @Model.Exists

+``` + +```html + +@model YourNamespace.Models.CountGreaterThanFiveViewModel +

Count of numbers greater than 5: @Model.Count

+``` + +```html + +@model YourNamespace.Models.SumAllElementsViewModel +

Sum of all elements: @Model.Sum

+``` + +```html + +@model YourNamespace.Models.MaxValueViewModel +

Maximum value: @Model.MaxValue

+``` + +```html + +@model YourNamespace.Models.MinValueViewModel +

Minimum value: @Model.MinValue

+``` + +```html + +@model YourNamespace.Models.AverageValueViewModel +

Average value: @Model.AverageValue

+``` + +```html + +@model YourNamespace.Models.DivisibleByThreeViewModel +
    + @foreach (var number in Model.DivisibleByThree) + { +
  • @number
  • + } +
+``` + +### 步骤 5: 运行和访问 + +1. 确保你的项目已经设置为启动项目。 +2. 运行项目(通常是按F5或点击VS Core中的“启动”按钮)。 \ No newline at end of file -- Gitee