diff --git "a/\350\251\271\345\256\207\350\210\252/20241202.md" "b/\350\251\271\345\256\207\350\210\252/20241202.md" new file mode 100644 index 0000000000000000000000000000000000000000..a6cf4ff9b5d285e52827cf671ef987a22a0d34a9 --- /dev/null +++ "b/\350\251\271\345\256\207\350\210\252/20241202.md" @@ -0,0 +1,28 @@ +# 步骤 +1. dotnet new mvc -o Blog (新建一个Blog文件) + +2. BlogsController(新建控制器) + - namespace Blog.Controllers;(控制器里的命名空间) + - using Microsoft.AspNetCore.Mvc;(最好记住) + - public class BlogsController:Controller{ + + public IActionResult Index(){ + return Views(); + } + } +3. 再创建对应的视图,文件夹名对应控制器名,视图下的cshtml文件名字对应控制器内部函数名字。 + - 在index.cshtml中写增删改查页面内容 +4. 在wwwroot文件夹下的css文件夹下建一个base.css文件link到index.cshtml中,可以写样式 + - `` +5. 在Model里增加Blogs.cs/Db.cs + - namespace Blog.Models;(这位model里的命名空间) + ``` + 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/\350\251\271\345\256\207\350\210\252/20241204.md" "b/\350\251\271\345\256\207\350\210\252/20241204.md" new file mode 100644 index 0000000000000000000000000000000000000000..56803c4368ca00f3688fc6c6db8f0b86e5159efb --- /dev/null +++ "b/\350\251\271\345\256\207\350\210\252/20241204.md" @@ -0,0 +1,86 @@ +# 新增页面 +*表单* +```csharp +@model Blog.Models.Blogs; + +
+
+
+
+ +
+``` +```csharp + + 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"); + 1. 已经验证表单数据可以传入。 + 2. 拿到传入数据后,一般做数据验证(用户名是否存在,必填,手机号格式,长度是否符合长度。。。)。 + 3. 若符合验证规则,则保存到数据库里否则提示验证不通过。 + 4. 若保存数据库成功。则跳转到表面,不通过,那仍然显示新增页面。 + } + + // 先通过select 拿到集合中的所有id,放在一个新的集合中返回,然后对这个返回的新的集合应用Max方法,找到其中最大值 + // var blogs = Db.Blogs.Where(x => x.Title.Equals(input.Title)); + // if (blogs.Count() > 0) + // { + // return View("create"); + // } +``` +# 编辑页面 +```csharp +@model Blog.Models.Blogs; + +
+
+
+
+
+ +
+``` +```csharp + public IActionResult Edit(int id) + { + // 根据id找到对应的blogs,有可能为空 + var blog = Db.Blogs.FirstOrDefault(x => x.Id == id); + return View(blog); + } + + [HttpPost] + public IActionResult Edit(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"); + } +``` + +# 三、Linq集成查询(关联Lambda) +First FirstOrDefaualt 找第一个符合条件的元素 + +First(x=>x.Id==id) 返回第一个Id等于id的元素,如果都没有符合的,报错 +FirstOrDefault(x=>x.Id==id) 返回第一个Id等于id的元素,如果都没有符合的,返回Null +Single SingleOrDefault + +Single() 返回第一个元素,如果没有,报错 +SingleOrDefault() 返回第一个元素,如果没有,返回Null +Where + +Where(x=>x.Score>=80 && x.Sex==1) 查找所有成绩大于等于80,并且性别为1的所有元素 +Select + +Select(x=>new {x.Id,x.Score}) 以新的{x.Id,x.Score}对象的形式,返回新的集合 +from XX in XX where XXX select; \ No newline at end of file diff --git "a/\350\251\271\345\256\207\350\210\252/20241205.md" "b/\350\251\271\345\256\207\350\210\252/20241205.md" new file mode 100644 index 0000000000000000000000000000000000000000..4be36cec441efcbdf65738aeec3951a55b31d4ef --- /dev/null +++ "b/\350\251\271\345\256\207\350\210\252/20241205.md" @@ -0,0 +1,77 @@ +## 删除 +1. 需要再Index.cshtml页面中`删除`获取Id值 +2. Delete.cshtml中的样式 + ``` + @model Blog.Models.Blogs; +

你真的要删除吗?

+ + + + + + + + + + + + + + + + + +
标题@Model.Title
内容@Model.Content
作者@Model.Author
删除回到主页
+ ``` +3. 展示删除页面 + ``` + public IActionResult Delete(int id) + { + //根据id找到对应的blog + var blog=Db.Blogs.FirstOrDefault(X=>X.Id==id); + return View(blog); + } + ``` +4. 确认删除页面 + ``` + public IActionResult DeleteConfirm(int id) + { + //根据id找到对应的blog + var blog=Db.Blogs.FirstOrDefault(X=>X.Id==id); + //如果blog不为空,查找出来就删除 + if(blog!=null) + { + Db.Blogs.Remove(blog); + //删除完返回列表页 + return RedirectToAction("Index"); + } + //如果找不到就返回找不到提示 + return NotFound(); + } + ``` + + +## 查询 +1. Index.cshtml的查询样式 + ``` +
+ + +
+ ``` +2. 查询页面 + ``` + public IActionResult Index(string keyword) + { + //判读keyword是否为空,不为空则删除空格 + keyword=string.IsNullOrEmpty(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); + } + ``` \ No newline at end of file