diff --git "a/\351\231\206\346\245\232\347\233\210/20241127Razor\345\237\272\346\234\254\346\246\202\345\277\265.md" "b/\351\231\206\346\245\232\347\233\210/20241127Razor\345\237\272\346\234\254\346\246\202\345\277\265.md" new file mode 100644 index 0000000000000000000000000000000000000000..c59a0a8222f5cf7d45de1d587bae7c252b55730e --- /dev/null +++ "b/\351\231\206\346\245\232\347\233\210/20241127Razor\345\237\272\346\234\254\346\246\202\345\277\265.md" @@ -0,0 +1,16 @@ +# Razor基本概念 +定义: Razor是一种标记语法,允许开发者在HTML中嵌入C#代码。它通过使用@符号来转换HTML和C#代码 +文件扩展名: 通常,Razor视图文件使用.cshtml扩展名,这意味着这些文件包含HTML和C#代码 +## Razor的功能 + +* 嵌入C#代码: Razor支持在HTML中直接编写C#代码,使得开发者可以动态生成内容。例如,可以使用@DateTime.Now来显示当前日期和时间 + +* 表达式和代码块: 隐式表达式: 以@开头,后跟C#代码,如@Model.Property。 显式表达式: 使用@()包裹C#表达式,以确保表达式正确解析 + +* 代码块: 使用@{}来包含多行C#代码,这些代码不会直接输出到HTML中,而是用于逻辑处理 + +* 数据绑定: Razor支持与模型(Model)的双向数据绑定,允许视图直接访问和显示模型数据 + +* 布局和部分视图: Razor支持布局(Layouts)和部分视图(Partial Views),这使得开发者可以重用代码并保持一致的页面结构 + +* 控制结构: Razor支持C#的控制结构,如@if、@for、@foreach等,这些结构允许开发者在视图中实现复杂的逻辑 \ No newline at end of file diff --git "a/\351\231\206\346\245\232\347\233\210/20241129\350\247\206\345\233\276\345\217\212\345\205\266\346\250\241\346\235\277\345\274\225\346\223\216.md" "b/\351\231\206\346\245\232\347\233\210/20241129\350\247\206\345\233\276\345\217\212\345\205\266\346\250\241\346\235\277\345\274\225\346\223\216.md" new file mode 100644 index 0000000000000000000000000000000000000000..93355b67171ddf158f5029d0014e44de72d369f5 --- /dev/null +++ "b/\351\231\206\346\245\232\347\233\210/20241129\350\247\206\345\233\276\345\217\212\345\205\266\346\250\241\346\235\277\345\274\225\346\223\216.md" @@ -0,0 +1,169 @@ +# 渲染简单数据 +``` + public string Easy() + { + return "这是一个简单数据类型"; + } +``` +# 渲染对象数据 +``` + public IActionResult Friend() + { + var list = new FriendCreateDto + { + Name="对象数据", + }; + return View(list); + } + public class FriendCreateDto{ + public string Name{get;set;}=null!; + } +``` +# 渲染集合数据 +``` + public IActionResult Listt() + { + var list = new List + { + new ListtCreateDto + { + Name="渲染", + Title="集合", + Content="数据" + }, + new ListtCreateDto + { + Name="渲染", + Title="集合", + Content="数据" + }, + }; + return View(list); + + } + + public class ListtCreateDto{ + public string Name{get;set;}=null!; + public string Title{get;set;}=null!; + public string Content{get;set;}=null!; + } +``` +``` + @foreach (var item in Model) + { +

姓名 @item.Name

+

标题 @item.Title

+

内容 @item.Content

+ } +``` +# 渲染保护集合数据的对象数据 +``` + +``` +# 尝试构建列表 +``` +BlogControll.cs + + using Microsoft.AspNetCore.Mvc; + using Blog.Models; + using System.Text.Json; + + namespace Blog.Controllers; + + public class BlogController : Controller + { + public IActionResult Index() + { + return View(Db.Think); + } + + public IActionResult Addd() + { + return View(); + } + [HttpPost] + public IActionResult Addd(Think t) + { + var maxId = Db.Think.Select(t => t.Id).Max(); + t.Id = maxId+1; + Db.Think.Add(t); + return RedirectToAction("index"); + } + public IActionResult Edit(){ + return View(); + } + public IActionResult Why(){ + return View(); + } + public IActionResult Delect(){ + return View(); + } + } + +``` +``` +Db.cs + +namespace Blog.Models; + public static class Db + { + public static List Think{get;set;} + static Db() + { + Think = new List(); + for (int i = 0; i < 4; i++) + { + var text = new Think{ + Id=i+1, + Title = "33333", + Content = "99999", + Author = "未知" + }; + Think.Add(text); + } + } + } + +Thinkk.cs + +namespace Blog.Models; + + public class Think{ + public int Id{get;set;} + public string Title{get;set;}=null!; + public string Content{get;set;}=null!; + public string Author{get;set;}=null!; + + } +``` +``` +Index.cshtml + + @model List + + + + + + + + + + + @foreach(var item in @Model) + { + + + + + + + + 编辑 + 详情 + 删除 + + } + +
ID标题内容作者
@item.Id@item.Title@item.Content@item.Author
+``` \ No newline at end of file