首页 > 解决方案 > 虚方法引用的类,返回 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 中的IndexBuildHomeIndex方法:

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; }
    }
}

标签: c#referencevirtualnullreferenceexception

解决方案


推荐阅读