gpt4 book ai didi

c# - ASP.NET MVC View 未接收传递给它的列表

转载 作者:太空宇宙 更新时间:2023-11-03 22:46:59 25 4
gpt4 key购买 nike

我正在 Visual Studio 中使用 C# 开发一个相对简单的 ASP.NET MVC 应用程序。在这种情况下,我试图将对象列表传递到我的 View 中,然后显示它们的名称,但我一直收到错误

Categories does not exist in the current context

这是 Controller :

public IActionResult Index()
{
List <CheeseCategory> categories = context.Categories.ToList();
return View(categories);
}

这是 View :

@model CheeseMVC.Models.CheeseCategory

@if (categories != null)
{
foreach(var category in categories)
{
<ul>@category</ul>
}
}

我以前做过这个,我不知道这里出了什么问题,有没有人有任何想法?

最佳答案

您传入了一个 List<CheeseCategory> ,但在你的 Razor 文件中,你说 model将只是一个 CheeseCategory . @model声明需要与您传递的内容相匹配。

另外,替换 categoriesModelif Razor 文件中的声明。

关于c# - ASP.NET MVC View 未接收传递给它的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49397671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com