【问题标题】:Not returning a list of data into index view in ASP.NET Core MVC不将数据列表返回到 ASP.NET Core MVC 中的索引视图中
【发布时间】:2020-12-27 13:37:21
【问题描述】:

我正在使用 ASP.NET Core MVC 对我自己的网站进行编程,并按照我想要迭代从我的数据库填充到视图的数据的视频教程之一。以下是代码:

存储库

public class ChoreBearRepository : IChoreBearRepo
{
    private readonly DatabaseContext _context;
    public ChoreBearRepository(DatabaseContext context)
    {
        _context = context;
    }

    /*
    This methods returns all categories from the database using the background thread.
    */
    public async Task<List<CategoryofTask>> GetAllCategories()
    {
        return await _context.categoryOfTasks
            .Select(category => new CategoryofTask(){
                CategoryName = category.CategoryName,
                CategoryDesc = category.CategoryDesc,
                CategoryID = category.CategoryID,
                CategoryImageURL = category.CategoryImageURL,
                CategoryRate = category.CategoryRate
            }).ToListAsync();
    }
}

控制器:

public class HomeController: Controller
{
    private readonly IChoreBearRepo _repository;

    public HomeController(IChoreBearRepo repository)
    {
       _repository = repository;
    }

    // public IActionResult Index()
    // {
    //     return View();
    // }

    [HttpGet]
    public async Task<IActionResult> Index()
    {
        var data = await _repository.GetAllCategories();
        return View(data);
    }
}

索引.cshtml

@page
@model IEnumerable<ChoreBear_Website.Models.CategoryofTask>

<div class="row">
    @foreach (var category in Model)
    {
        <partial name="Categories" model=@Model/>
    }
</div>

部分视图(Categories.cshtml)

@model CategoryofTask

<div class="card" style="width: 18rem;">
    <img class="card-img-top" src="@Model.CategoryImageURL" alt="Card image cap">
    <div class="card-body">
        <h5 class="card-title">@Model.CategoryName</h5>
        <p class="card-text">@Model.CategoryDesc</p>
        <a href="#" class="btn btn-primary">Go somewhere</a>
    </div>
</div>

该列表包含来自数据库的数据,但网站项目向我返回了一个错误:

NullReferenceException:对象引用未设置为 目的。 MyApp.Namespace.Home.Views_Home_Index.get_Model()

我知道列表是空的,这就是为什么我想知道我应该在哪里初始化列表。我按照视频教程中的代码示例进行操作。

PS:我从数据库中关注了这个视频教程Get List of Data

【问题讨论】:

  • 你试过调试吗? (一步一步)这将有助于轻松找到这种错误(如果不涉及多线程)。
  • @Hopeless 您好,由于我使用的是 VS Code,因此无法逐步调试。有什么办法吗?谢谢

标签: c# asp.net-core entity-framework-core asp.net-core-mvc


【解决方案1】:

您的代码看起来可以检索列表并将其返回到 VIEW“索引”,即使该列表是空的,因为数据库中没有记录。

但是,有两个问题。您的 Index.cshtml 有一个“@page”指令,这意味着它现在是 Razor 页面而不是视图,并希望在 Index.cshtml.cs 后面的页面或同一页面的 @code 部分中找到模型。这是您的错误的根源。

此外,在下文中,您将 IEnumerable 作为模型传递给部分视图,这需要一个单独的 CategoryOfTask ...

@model IEnumerable<ChoreBear_Website.Models.CategoryofTask>

<div class="row">
    @foreach (var category in Model)
    {
        <partial name="Categories" model=@Model/>
    }
</div>

尝试改变...

<partial name="Categories" model=@Model/>

到...

<partial name="Categories" model=@category/>

要了解 Razor 视图和页面之间的区别,请参阅此链接:

https://docs.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-5.0&tabs=visual-studio

前面的代码看起来很像 Razor 视图文件 带有控制器和视图的 ASP.NET Core 应用程序。是什么让它与众不同 是@page 指令。 @page 使文件成为 MVC 动作 - 这意味着它直接处理请求,而不经过 控制器。 @page 必须是页面上的第一个 Razor 指令。 @页 影响其他 Razor 构造的行为。 Razor Pages 文件名 有一个 .cshtml 后缀。

【讨论】:

  • 嗨@Neil W,按照你的建议做了。它没有解决问题。在“@foreach(模型中的 var 类别)”处出现错误。
  • 尝试从 Index.cshtml 的第一行删除“@page”。
  • 已经用更完整的描述编辑了答案。
  • 我删除了它并且它起作用了。也感谢您的链接。现在,我知道为什么 @page 指令会这样做了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-18
  • 1970-01-01
  • 2022-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多