diff --git "a/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\347\254\224\350\256\260/20241125-mvc\345\272\217\345\210\227.md" "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\347\254\224\350\256\260/20241125-mvc\345\272\217\345\210\227.md" new file mode 100644 index 0000000000000000000000000000000000000000..37409d344e8a48b4900fff9030217354e6ab75d8 --- /dev/null +++ "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\347\254\224\350\256\260/20241125-mvc\345\272\217\345\210\227.md" @@ -0,0 +1,20 @@ +# MVC中的序列化与反序列化 + +## 序列化(Serialization) +序列化是将对象的状态信息转换为可以存储或传输的格式(如JSON、XML等)的过程。 + +### 为什么需要序列化? +- **存储**:将对象状态保存到数据库或文件中。 +- **网络传输**:在网络上发送对象数据,如API响应。 + +### 如何进行序列化? +```javascript +// 假设有一个简单的JavaScript对象 +var user = { + id: 1, + name: "John Doe", + email: "john@example.com" +}; + +// 将对象序列化为JSON字符串 +var serializedUser = JSON.stringify(user); \ No newline at end of file diff --git "a/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\347\254\224\350\256\260/20241127-mvc.md" "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\347\254\224\350\256\260/20241127-mvc.md" new file mode 100644 index 0000000000000000000000000000000000000000..afaa3a73efe103193aaae2d61598f02696d35e33 --- /dev/null +++ "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\347\254\224\350\256\260/20241127-mvc.md" @@ -0,0 +1,72 @@ +、Action在MVC中的角色 +Action:在MVC模式中,Action指的是控制器中的一个方法或函数,它负责处理用户请求并返回相应的响应。 +特点:在控制台应用程序中,Action方法通常读取用户输入,调用模型处理数据,然后通过视图输出结果。 +四、MVC模式在控制台应用程序中的实现 +## 1. 定义模型 +创建一个包含业务逻辑和数据结构的类。 +csharp +// 示例模型类 +```public class CalculatorModel +{ + public int Add(int a, int b) + { + return a + b; + } +} +``` +## 2. 定义视图 +创建一个用于显示数据的类。 +csharp +// 示例视图类 +```public class CalculatorView +{ + public void DisplayResult(int result) + { + Console.WriteLine("The result is: " + result); + } +} +``` +## 3. 定义控制器(包含Action) +创建一个包含主逻辑的函数或类,负责读取用户输入,调用模型处理数据,并通过视图输出结果。 + +``` +public class CalculatorController +{ + private CalculatorModel model; + private CalculatorView view; + + public CalculatorController() + { + model = new CalculatorModel(); + view = new CalculatorView(); + } + + // Action方法 + public void PerformAddition() + { + Console.Write("Enter first number: "); + int num1 = int.Parse(Console.ReadLine()); + + Console.Write("Enter second number: "); + int num2 = int.Parse(Console.ReadLine()); + + // 调用模型处理数据 + int result = model.Add(num1, num2); + + // 通过视图输出结果 + view.DisplayResult(result); + } +} +``` +## 4. 主函数 +在主函数中创建控制器的实例,并调用Action方法。 + +```class Program +{ + static void Main(string[] args) + { + CalculatorController controller = new CalculatorController(); + controller.PerformAddition(); // 调用Action方法 + } +} +``` \ No newline at end of file diff --git "a/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\347\254\224\350\256\260/20241128-mvc2.md" "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\347\254\224\350\256\260/20241128-mvc2.md" new file mode 100644 index 0000000000000000000000000000000000000000..5d0d70316b2d1275d65be7ef664ad47a54cb3e3f --- /dev/null +++ "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\347\254\224\350\256\260/20241128-mvc2.md" @@ -0,0 +1,176 @@ +![20241129220600](http://www.lamianeatpp.top/zhao/20241129220600.png) +![20241129220642](http://www.lamianeatpp.top/zhao/20241129220642.png) +![20241129230342](http://www.lamianeatpp.top/zhao/20241129230342.png) +``` + public IActionResult Baijia() + // { + + // return Content(MakeNa() + MakeMe()); + + // } + + // private string MakeNa() + // { + + // var name = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常于时傅皮卞齐康伍余元卜顾孟平黄和穆萧尹"; + // var rand = new Random(); + // var index = rand.Next(1, 101); + // var a = name[index]; + + // return a.ToString(); + + // } + + // private string MakeMe() + // { + + // var name = "赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常于时傅皮卞齐康伍余元卜顾孟平黄和穆萧尹"; + // var rand = new Random(); + + // var mes = rand.Next(1, 3); + // string a = ""; + // for (int i = 0; i < mes; i++) + // { + // var index = rand.Next(1, 101); + // a += name[index]; + // } + + + // return a.ToString(); + + // } +``` + +![20241129230211](http://www.lamianeatpp.top/zhao/20241129230211.png) + +```model +public class BlogCreateDto +{ + + + public string Title { get; set; } = null!; + public string Author { get; set; } = null!; + public string Content { get; set; } = null!; + + +} +``` + +```controller +using System.Diagnostics; +using Microsoft.AspNetCore.Mvc; +using Blog.Models; +using System.Collections.Generic; + + +namespace Blog.Controllers; + +public class ListController : Controller +{ + + public IActionResult Index() + { + + return View(); + + } + + public dynamic Lie() + { + + var list = new List(); + + + // new BlogCreateDto{ + // Title=CreTitle(), + // Author=CreAuthor(), + // Content=CreContent(), + + // } + + + for (int i = 0; i < 100; i++) + { + list.Add(new BlogCreateDto + { + Title = CreTitle(), + Author = CreAuthor(), + Content = CreContent(), + }); + } + + + + + + return View(list); + + + + } + + + + + //--------------------------------------------------- + public int ContentNum() + { + + var rand = new Random(); + var a = rand.Next(1, 10); + return a; + } + + public string ContentText() + { + + var rand = new Random(); + var a = rand.Next(1, 100); + var text = "李张王刘陈杨赵黄周吴徐孙胡朱高林何郭马罗梁宋郑谢韩唐冯于董萧程曹袁邓许傅沈曾彭吕苏卢蒋蔡贾丁魏薛叶阎余潘杜戴夏钟汪田任姜范方石姚谭廖邹熊金陆郝孔白崔康毛邱秦江史顾侯邵孟龙万段雷钱汤尹黎易常武乔贺赖龚文"; + return text[a].ToString(); + } + + public string CreTitle() + { + + var length = ContentNum(); + var title = ""; + for (int i = 0; i < length; i++) + { + title += ContentText(); + } + return title.ToString(); + } + public string CreAuthor() + { + + var length = ContentNum(); + var author = ""; + for (int i = 0; i < length; i++) + { + author += ContentText(); + } + return author.ToString(); + } + public string CreContent() + { + + var length = ContentNum(); + var content = ""; + for (int i = 0; i < length; i++) + { + content += ContentText(); + } + return content.ToString(); + } + public dynamic Abc() + { + + var a = CreContent(); + var b = CreTitle(); + return View(); + + } + +} +``` \ No newline at end of file