c# - 虚方法引用的类,返回 null ASP.Net Core MVC
问题描述
我带着问题来了。
为什么我的类返回空值?
正如您在这张图片上看到的那样,类返回 null,但里面的字段不是 null。
这是我将虚拟方法放置到类 Category 的 Post 模型,并且该类为 null
为什么会这样?
我检查了每一个参考,一切都很好。这可能是由 Windows 更新或 IIS/VisualStudio 引起的?
请帮忙
后模型
using System;
using System.Collections.Generic;
namespace collector_forum.Data.Models
{
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public DateTime Created { get; set; }
public virtual ApplicationUser User { get; set; }
public virtual Category Category { get; set; }
public virtual IEnumerable<PostReply> Replies { get; set; }
}
}
类别型号:
using System;
using System.Collections.Generic;
namespace collector_forum.Data.Models
{
public class Category
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public DateTime Created { get; set; }
public string ImageUrl { get; set; }
public virtual IEnumerable<Post> Posts { get; set; }
}
}
HomeController 中的Index和BuildHomeIndex方法:
public IActionResult Index()
{
var model = BuildHomeIndexModel();
return View(model);
}
public HomeIndexModel BuildHomeIndexModel()
{
var latestPosts = _postService.GetLatestPosts(10);
var posts = latestPosts.Select(post => new PostListingModel
{
Id = post.Id,
Title = post.Title,
AuthorId = post.User.Id,
AuthorName = post.User.UserName,
AuthorRating = post.User.Rating,
DatePosted = post.Created.ToString(),
RepliesCount = post.Replies.Count(),
Category = GetCategoryListingForPost(post)
});
return new HomeIndexModel
{
LatestPosts = posts,
SearchQuery = ""
};
}
PostListingModel(如果需要):
using collector_forum.Models.Category;
namespace collector_forum.Models.Post
{
public class PostListingModel
{
public CategoryListingModel Category { get; set; }
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string AuthorName { get; set; }
public int AuthorRating { get; set; }
public string AuthorId { get; set; }
public string DatePosted { get; set; }
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public string CategoryImageUrl { get; set; }
public int RepliesCount { get; set; }
}
}
CategoryListingModel(如果需要):
using collector_forum.Models.Post;
using System.Collections.Generic;
namespace collector_forum.Models.Category
{
public class CategoryListingModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string ImageUrl { get; set; }
public int NumberOfPosts { get; set; }
public int NumberOfUsers { get; set; }
public bool HasRecentPost { get; set; }
public PostListingModel Latest { get; set; }
}
}
解决方案
推荐阅读
- javascript - 如何使用 querySelector 点击 svg?
- java - 如何在 JEE 实体管理器中使用来自 testcontainer 的数据源
- c# - 如何将 XML 字符串替换为另一个 XML 字符串
- scala - 无法在 Spark 中使用带有管道分隔符的 str_to_map
- microcontroller - 可以“交换 nybble”和“字节掩码”技巧将多字节逻辑移位 4 比使用移位链的天真方法快得多
- python - Pygame 按键与指南的工作方式不同
- autodesk-forge - 关闭剖面线填充
- c# - EF Core 中必需到可选关系(一对零或一)的最佳方法是什么?
- android - 如何使用反应原生视频从内部存储访问我的视频?
- c# - 如何在 Blazor 中使用 Virtualize 组件实现分组(可扩展组)?