diff --git "a/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\344\275\234\344\270\232/20241205-\344\275\234\344\270\232/Blogs.cs" "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\344\275\234\344\270\232/20241205-\344\275\234\344\270\232/Blogs.cs" new file mode 100644 index 0000000000000000000000000000000000000000..d2f38f13555e5db51f00db87bbaa3570997678db --- /dev/null +++ "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\344\275\234\344\270\232/20241205-\344\275\234\344\270\232/Blogs.cs" @@ -0,0 +1,9 @@ +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!; +} \ No newline at end of file diff --git "a/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\344\275\234\344\270\232/20241205-\344\275\234\344\270\232/Blogs/Create.cshtml" "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\344\275\234\344\270\232/20241205-\344\275\234\344\270\232/Blogs/Create.cshtml" new file mode 100644 index 0000000000000000000000000000000000000000..7a836150bbe2aa77c9f1c75bd2c1528f06224fda --- /dev/null +++ "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\344\275\234\344\270\232/20241205-\344\275\234\344\270\232/Blogs/Create.cshtml" @@ -0,0 +1,67 @@ + @model Blog.Models.Blogs; + + + + + 增加页面 + + + + + + +
+
+
+
+ +
+ + \ No newline at end of file diff --git "a/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\344\275\234\344\270\232/20241205-\344\275\234\344\270\232/Blogs/Edit.cshtml" "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\344\275\234\344\270\232/20241205-\344\275\234\344\270\232/Blogs/Edit.cshtml" new file mode 100644 index 0000000000000000000000000000000000000000..3f5612063bc04af8bc99d426d8878b65573eafb8 --- /dev/null +++ "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\344\275\234\344\270\232/20241205-\344\275\234\344\270\232/Blogs/Edit.cshtml" @@ -0,0 +1,68 @@ + @model Blog.Models.Blogs; + + + + + 编辑页面 + + + + + + +
+
+
+
+
+ +
+ + \ No newline at end of file diff --git "a/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\344\275\234\344\270\232/20241205-\344\275\234\344\270\232/Blogs/Index.cshtml" "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\344\275\234\344\270\232/20241205-\344\275\234\344\270\232/Blogs/Index.cshtml" new file mode 100644 index 0000000000000000000000000000000000000000..c8391ab887d5fcad48b45a0c0d9349ab4fbf3469 --- /dev/null +++ "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\344\275\234\344\270\232/20241205-\344\275\234\344\270\232/Blogs/Index.cshtml" @@ -0,0 +1,282 @@ +@model List; + + + + + + + + + 增删改查表 + + + + + + + + +
+ +
+ +
+
+ + + + + +
+
+
+ +
+
+ +
+
+
+ + + + + + + + + + + + + + @foreach (var item in @Model) + { + + + + + + + + + + } + + + + +
Id标题内容作者 + 操作 +
@item.Id@item.Title@item.Content@item.Author + 编辑 + + 删除 +
+
+ +
+ + + + + \ No newline at end of file diff --git "a/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\344\275\234\344\270\232/20241205-\344\275\234\344\270\232/BlogsController.cs" "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\344\275\234\344\270\232/20241205-\344\275\234\344\270\232/BlogsController.cs" new file mode 100644 index 0000000000000000000000000000000000000000..7f842aaa298a869ea2ba8f11274ae2672048d861 --- /dev/null +++ "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\344\275\234\344\270\232/20241205-\344\275\234\344\270\232/BlogsController.cs" @@ -0,0 +1,89 @@ +using Blog.Models; +using Microsoft.AspNetCore.Mvc; +using System.Linq; + + +namespace Blog.Controllers; + + +public class BlogsController : Controller +{ + + public IActionResult Index(string keyword) + { + + if (string.IsNullOrEmpty(keyword)) + { + return View(Db.Blogs); + } + else{ + + keyword=keyword.Trim(); + var list = Db.Blogs.Where(x => x.Title.Contains(keyword) + || x.Content.Contains(keyword) + || x.Author.Contains(keyword)).ToList(); + return View(list); + } + + + } + public IActionResult Create() + { + return View(); + } + + [HttpPost] + public IActionResult Create(Blogs input) + { + + var maxId = Db.Blogs.Select(x => x.Id).Max(); + input.Id = maxId + 1; + Db.Blogs.Add(input); + return RedirectToAction("Index"); + + } + [HttpPost] + + public IActionResult Edit(Blogs input) + { + + 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 Edit(int id) + { + var blog = Db.Blogs.FirstOrDefault(x => x.Id == id); + return View(blog); + } + + // [HttpPost] + // public IActionResult Index(Blogs input) + // { + + // var blog=Db.Blogs.FirstOrDefault(x=>x.Title==input.Title); + + // return View(blog); + // } + + public IActionResult Delete(int id) + { + var blog = Db.Blogs.FirstOrDefault(x => x.Id == id); + if (blog == null) + { + return View(); + } + Db.Blogs.Remove(blog); + return RedirectToAction("Index"); + } + + +} \ No newline at end of file diff --git "a/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\344\275\234\344\270\232/20241205-\344\275\234\344\270\232/Db.cs" "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\344\275\234\344\270\232/20241205-\344\275\234\344\270\232/Db.cs" new file mode 100644 index 0000000000000000000000000000000000000000..b7f0b1e891193476f790772d930feff16948732f --- /dev/null +++ "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\344\275\234\344\270\232/20241205-\344\275\234\344\270\232/Db.cs" @@ -0,0 +1,58 @@ +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 = Cre(), + Content = Cre(), + Author = Cre() + }; + + Blogs.Add(tmp); + } + + + } + + public static int ContentNum() + { + + var rand = new Random(); + var a = rand.Next(1, 10); + return a; + } + + public static string ContentText() + { + + var rand = new Random(); + var a = rand.Next(1, 100); + var text = "李张王刘陈杨赵黄周吴徐孙胡朱高林何郭马罗梁宋郑谢韩唐冯于董萧程曹袁邓许傅沈曾彭吕苏卢蒋蔡贾丁魏薛叶阎余潘杜戴夏钟汪田任姜范方石姚谭廖邹熊金陆郝孔白崔康毛邱秦江史顾侯邵孟龙万段雷钱汤尹黎易常武乔贺赖龚文"; + return text[a].ToString(); + } + public static string Cre() + { + + var length = ContentNum(); + var title = ""; + for (int i = 0; i < length; i++) + { + title += ContentText(); + } + return title.ToString(); + } + + + +} + diff --git "a/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\347\254\224\350\256\260/20241202-mvc\345\201\232\344\270\200\344\270\252\345\210\227\350\241\250.md" "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\347\254\224\350\256\260/20241202-mvc\345\201\232\344\270\200\344\270\252\345\210\227\350\241\250.md" new file mode 100644 index 0000000000000000000000000000000000000000..c3569be5460e55c476c086a2abf7ecd2c7b6a148 --- /dev/null +++ "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\347\254\224\350\256\260/20241202-mvc\345\201\232\344\270\200\344\270\252\345\210\227\350\241\250.md" @@ -0,0 +1,134 @@ +## 1. 创建一个内存中的数据存储 +首先,您需要一个在内存中存储数据的方式。这可以是一个简单的列表或者字典。 + +``` +public class MemoryDataStore +{ + public static List Items = new List(); + + public static void AddItem(Item item) + { + Items.Add(item); + } + + public static void UpdateItem(int id, Item updatedItem) + { + var item = Items.FirstOrDefault(i => i.Id == id); + if (item != null) + { + item.Title = updatedItem.Title; + item.Content = updatedItem.Content; + item.Author = updatedItem.Author; + } + } + + public static Item GetItem(int id) + { + return Items.FirstOrDefault(i => i.Id == id); + } +} +``` +## 2. 创建模型 +创建一个模型类来表示您的数据。 + +``` +public class Item +{ + public int Id { get; set; } + public string Title { get; set; } + public string Content { get; set; } + public string Author { get; set; } +} +``` +## 3. 创建控制器 +在控制器中,添加方法来处理增加和编辑的逻辑。 + +``` +public class ItemsController : Controller +{ + // GET: Items/Create + public ActionResult Create() + { + return View(); + } + + // POST: Items/Create + [HttpPost] + public ActionResult Create(Item item) + { + if (ModelState.IsValid) + { + MemoryDataStore.AddItem(item); + return RedirectToAction("Index"); + } + + return View(item); + } + + // GET: Items/Edit/5 + public ActionResult Edit(int id) + { + Item item = MemoryDataStore.GetItem(id); + if (item == null) + { + return HttpNotFound(); + } + return View(item); + } + + // POST: Items/Edit/5 + [HttpPost] + public ActionResult Edit(int id, Item item) + { + if (ModelState.IsValid) + { + MemoryDataStore.UpdateItem(id, item); + return RedirectToAction("Index"); + } + return View(item); + } +} +``` +## 4. 创建视图 +为创建和编辑操作创建相应的视图。 + +`Create.cshtml` + +``` +@model Item +
+
+ + +
+
+ + +
+
+ + +
+ +
+Edit.cshtml + +html +@model Item +
+
+ + +
+
+ + +
+
+ + +
+ + +
+``` diff --git "a/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\347\254\224\350\256\260/20241204-mvc\345\242\236\345\210\240\346\224\271\346\237\2451.md" "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\347\254\224\350\256\260/20241204-mvc\345\242\236\345\210\240\346\224\271\346\237\2451.md" new file mode 100644 index 0000000000000000000000000000000000000000..c7c9307a8f5acf9d8afc965ad1e2f36e167ecd69 --- /dev/null +++ "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\347\254\224\350\256\260/20241204-mvc\345\242\236\345\210\240\346\224\271\346\237\2451.md" @@ -0,0 +1,35 @@ +## 5. 处理数据的显示 +您可以在控制器中添加一个Index方法来显示所有项目,并创建相应的视图。 + +``` +public ActionResult Index() +{ + return View(MemoryDataStore.Items); +} +``` +`Index.cshtml` + +``` +@model IEnumerable + + + + + + + + @foreach (var item in Model) + { + + + + + + + } +
标题内容作者操作
@item.Title@item.Content@item.Author + 编辑 +
+``` +这样,您就可以在不连接数据库的情况下实现增加和编辑功能。请注意,这些数据将在应用程序重启后丢失,因为它们只存储在内存中。 + diff --git "a/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\347\254\224\350\256\260/20241205-mvc\345\242\236\345\210\240\346\224\271\346\237\2452.md" "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\347\254\224\350\256\260/20241205-mvc\345\242\236\345\210\240\346\224\271\346\237\2452.md" new file mode 100644 index 0000000000000000000000000000000000000000..5bca76182c27c28a9f2b2424815fc013f59325a8 --- /dev/null +++ "b/\351\202\271\346\260\270\346\266\233/\350\257\276\345\240\202\347\254\224\350\256\260/20241205-mvc\345\242\236\345\210\240\346\224\271\346\237\2452.md" @@ -0,0 +1,92 @@ + + +``` +public class MemoryDataStore +{ + public static List Items = new List(); + + public static void AddItem(Item item) + { + Items.Add(item); + } + + public static void UpdateItem(int id, Item updatedItem) + { + var item = Items.FirstOrDefault(i => i.Id == id); + if (item != null) + { + item.Title = updatedItem.Title; + item.Content = updatedItem.Content; + item.Author = updatedItem.Author; + } + } + + public static Item GetItem(int id) + { + return Items.FirstOrDefault(i => i.Id == id); + } + + public static List SearchItems(string searchQuery) + { + return Items.Where(i => i.Title.Contains(searchQuery) || i.Content.Contains(searchQuery) || i.Author.Contains(searchQuery)).ToList(); + } +} +``` +## 2. 创建控制器方法 +在控制器中添加一个方法来处理查询请求: + +``` +public class ItemsController : Controller +{ + // GET: Items/Search + public ActionResult Search(string query) + { + var searchResults = MemoryDataStore.SearchItems(query); + return View(searchResults); + } +} +``` +## 3. 创建视图 +为查询操作创建一个视图,允许用户输入查询条件: + +`Search.cshtml` + +``` +@{ + ViewBag.Title = "Search"; +} + +

Search

+ +
+
+ + +
+ +
+ +

搜索结果:

+@if (Model == null || !Model.Any()) +{ +

没有找到匹配的项目。

+} +else +{ + + + + + + + @foreach (var item in Model) + { + + + + + + } +
标题内容作者
@item.Title@item.Content@item.Author
+} +``` \ No newline at end of file