From d3475560ceb15f0dafe36c31bcb1ec73503e5a9b Mon Sep 17 00:00:00 2001 From: Yu <2294831387@qq.com> Date: Wed, 26 May 2021 16:34:01 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=94=E8=AE=B0=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2021-05-17.md | 56 +++++++++++++++++++++++++++++++++++++++++++++ 2021-05-18.md | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2021-05-20.md | 35 ++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 2021-05-17.md create mode 100644 2021-05-18.md create mode 100644 2021-05-20.md diff --git a/2021-05-17.md b/2021-05-17.md new file mode 100644 index 0000000..b0d7393 --- /dev/null +++ b/2021-05-17.md @@ -0,0 +1,56 @@ +## 创建四个模型 +```c# + public abstract class BaseModel + { + public int Id { get; set; } + public DateTime CreatedAt { get; set; } + public DateTime UpdatedAt { get; set; } + + public int Version { get; set; } + public string Remarks { get; set; } + } + + public class Comments:BaseModel + { + public int MsgId { get; set; } + public int FromUserId { get; set; } + public string Content { get; set; } + } + + public class Messages:BaseModel + { + public int FromUserId { get; set; } + public string Content { get; set; } + } + + public class Users:BaseModel + { + public string Username { get; set; } + + public string Password { get; set; } + } +``` +## 创建一个ViewModel文件夹 +```c# + public class CommentViewModel + { + + public string FromUserName { get; set; } + public string Comment { get; set; } + } + + public class MessageViewModel + { + public int Id { get; set; } + public int FromUserId { get; set; } + + public string Content { get; set; } + public IEnumerable Comments { get; set; } + } + + public class SayViewModel + { + public string Username { get; set; } + public IEnumerable Messages { get; set; } + } +``` \ No newline at end of file diff --git a/2021-05-18.md b/2021-05-18.md new file mode 100644 index 0000000..8276d4f --- /dev/null +++ b/2021-05-18.md @@ -0,0 +1,63 @@ +## 控制器Messages +```c# + public class MessagesController : Controller + { + private SayDemoDb db = new SayDemoDb(); + + // GET: Messages + public ActionResult Index() + { + var id = 1; + var currentUser = db.Users.Where(x=>x.Id==id).SingleOrDefault(); + + // 获取所有的用户 + var userList = db.Users.ToList(); + // 获取所有的说说 + var msgList = db.Messages.ToList(); + // 所有的评论 + var comList = db.Comments.ToList(); + + // 定义一个准备用来放所有的首页消息模型的集合 + var msgViewModelList = new List(); + + // 遍历所有的消息 + foreach(var msg in msgList) + { + // 定义一个集合,用来存放当前消息下的所有的评论 + var tmpComList = new List(); + // 根据当前说说id,查找所有的评论 + var currentMsgComments = comList.Where(x => x.MsgId == msg.Id).ToList(); + + + // 遍历说说的所有评论 + foreach(var c in currentMsgComments) + { + tmpComList.Add(new CommentViewModel + { + FromUserName = userList.SingleOrDefault(x => x.Id == c.FromUserId).Username,// 根据当前的评论用户id,从用户中查找对应用户,并且获得用户的名称 + ToUserName = userList.SingleOrDefault(x => x.Id == msgList.SingleOrDefault(t => t.Id == c.MsgId).FromUserId).Username,// + Comment = c.Content + }); + } + + // 转换说说首页 + msgViewModelList.Add(new MessageViewModel + { + Id = msg.Id, + FromUserId = msg.FromUserId, + Content = msg.Content, + Comments = tmpComList + }); + } + + + var data = new SayViewModel + { + Username = currentUser.Username, + Messages = msgViewModelList + }; + + + return View(data); + } +``` diff --git a/2021-05-20.md b/2021-05-20.md new file mode 100644 index 0000000..8c92641 --- /dev/null +++ b/2021-05-20.md @@ -0,0 +1,35 @@ +## 首页视图 +```c# +
+

当前用户:@Model.Username

+
+
+
+
+
@msg.Content
+
+ +
+
+
+
+
    + @foreach(var comment in msg.Comments) + { +
  • +
    +

    @comment.Comment

    + +
    +
  • + } +
+
+``` \ No newline at end of file -- Gitee