Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc ASP.NET MVC核心NullReferenceException中的嵌套foreach循环_Asp.net Mvc_Razor_Asp.net Core Mvc - Fatal编程技术网

Asp.net mvc ASP.NET MVC核心NullReferenceException中的嵌套foreach循环

Asp.net mvc ASP.NET MVC核心NullReferenceException中的嵌套foreach循环,asp.net-mvc,razor,asp.net-core-mvc,Asp.net Mvc,Razor,Asp.net Core Mvc,我试图在一个有两个主要模型的简单web应用程序中添加导航侧栏,当我尝试使用嵌套循环在侧栏中呈现数据时,遇到了一个特殊的错误 我的应用程序有两个模型,如下面示例中的类别和内容: public class Category { public int ID { get; set; } public string Name { get; set; } public virtual ICollection<Thing> Things { get; set; } } p

我试图在一个有两个主要模型的简单web应用程序中添加导航侧栏,当我尝试使用嵌套循环在侧栏中呈现数据时,遇到了一个特殊的错误

我的应用程序有两个模型,如下面示例中的类别和内容:

public class Category
{
    public int ID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Thing> Things { get; set; }
}

public class Thing
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public int CategoryID { get; set; }
    public virtual Category Category { get; set; }
}
公共类类别
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection对象{get;set;}
}
公共类事物
{
公共int ID{get;set;}
公共字符串标题{get;set;}
公共字符串说明{get;set;}
public int CategoryID{get;set;}
公共虚拟类别{get;set;}
}
我想添加一个侧边栏,其中包含我所有类别的列表。我在_Layout.cshtml中创建了一个ViewComponent,如下所示:

// SidebarViewComponent.cs
public class SidebarViewComponent : ViewComponent
{
    private readonly MyContext db;

    public SidebarViewComponent(MyContext context)
    {
        db = context;
    }

    public async Task<IViewComponentResult> InvokeAsync()
    {
        var categories = await GetCategoriesAsync();
        return View(categories);
    }

    private Task<List<Category>> GetCategoriesAsync()
    {
        return db.Category.ToListAsync();
    }
}

// View file for SidebarViewComponent
@model IEnumerable<MyApp.Models.Category>
<div id="sidebar">
    @foreach (var category in Model)
    {
        <div class="header">@category.Name</div>
            @foreach (var thing in @category.Things)
            {
                <div>@thing.Title</div>
            }
        </div>
    }
</div>

// _Layout.cshtml
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>@ViewData["Title"]</title>

    <!-- Stylesheets -->
    <link rel="stylesheet" href="~/css/site.css">
</head>
<body>
    @await Component.InvokeAsync("Sidebar")
    @RenderBody()
</body>
</html>
//SidebarViewComponent.cs
公共类SidebarViewComponent:ViewComponent
{
私有只读MyContext数据库;
public SidebarViewComponent(MyContext上下文)
{
db=上下文;
}
公共异步任务InvokeAsync()
{
var categories=等待GetCategoriesAsync();
返回视图(类别);
}
私有任务GetCategoriesAsync()
{
返回db.Category.toListSync();
}
}
//SidebarViewComponent的视图文件
@模型IEnumerable
@foreach(模型中的var类别)
{
@类别.名称
@foreach(var thing in@category.Things)
{
@东西,头衔
}
}
//_Layout.cshtml
@ViewData[“标题”]
@wait Component.InvokeAsync(“侧栏”)
@RenderBody()
当我访问由ThingController呈现的页面时,它工作得非常好。但在我的主页或CategoryController处理的任何页面上,我都会在
@foreach(var thing in@category.Things)
行收到错误“NullReferenceException:对象引用未设置为对象的实例”


如何使此侧边栏在每页上都工作?

好的,我可以通过将ViewComponent的
GetCategoriesAsync
方法更改为使用相关集合的即时加载来工作:

private Task<List<Category>> GetCategoriesAsync()
{
    return db.Category.Include(c => c.Things).ToListAsync();
}
私有任务GetCategoriesAsync()
{
返回db.Category.Include(c=>c.Things.toListSync();
}

为我指出了正确的方向。

好的,我可以通过更改ViewComponent的
GetCategoriesAsync
方法来使用相关集合的即时加载来实现这一点:

private Task<List<Category>> GetCategoriesAsync()
{
    return db.Category.Include(c => c.Things).ToListAsync();
}
私有任务GetCategoriesAsync()
{
返回db.Category.Include(c=>c.Things.toListSync();
}

给我指出了正确的方向。

我找不到
ThingController
CategoryController
的任何代码,你能提供吗?另外,您是否确定
公共虚拟ICollection Things{get;set;}
是对
公共虚拟ICollection Things{get;set;}
的正确属性声明?我怀疑
CategoryController
在传递到视图时没有提供
Things
集合,因此foreach循环抛出NRE。我修复了问题中
Things
集合的声明。它在我的真实代码中已经是正确的,但我在我的MWE中打错了。我的控制器类是Visual Studio 2017在添加新控制器时创建的样板文件。我找不到
ThingController
&
CategoryController
,您能提供它吗?另外,您是否确定
公共虚拟ICollection Things{get;set;}
是对
公共虚拟ICollection Things{get;set;}
的正确属性声明?我怀疑
CategoryController
在传递到视图时没有提供
Things
集合,因此foreach循环抛出NRE。我修复了问题中
Things
集合的声明。它在我的真实代码中已经是正确的,但我在我的MWE中打错了。我的控制器类是Visual Studio 2017在添加新控制器时创建的样板文件。