System.NullReferenceException: 'Object reference not set to an instance of an object.'

Jiale Xue - MSFT 34,356 信誉分 Microsoft 供应商
2024-04-05T07:19:21.74+00:00

这个错误让我,我首先使用代码: System.NullReferenceException: '对象引用未设置为对象的实例。 el IEnumerable<BrentStat5.Context.CorvetteCost> @{ ViewData[“title”] = “Index”; } <h1>Index</h1> <p> <a asp-action=“Create”>Create New</a> </p> <table class=“table”> <thead> <tr> <th> @azzedinehtmlsql .DisplayNameFor(model => 模型。CorvetteItem) </th> <th> @azzedinehtmlsql .DisplayNameFor(model => 模型。CorvetteItemCost) </th> <th> @azzedinehtmlsql .DisplayNameFor(model => 模型。CorvetteStore) </th> <th> @azzedinehtmlsql .DisplayNameFor(model => 模型。修复)</th> <th> @azzedinehtmlsql .DisplayNameFor(model => 模型。升级)</th> <th> @azzedinehtmlsql .DisplayNameFor(model => 模型。日期)</th> <th> @azzedinehtmlsql .DisplayNameFor(model => 模型。维护) </th> <th> @azzedinehtmlsql .DisplayNameFor(model => 模型。OrginalCost) </th> <th></th> </tr> </thead> <tbody> @foreach (模型中的 var 项) { //error 在此行上。 <tr> <td> @azzedinehtmlsql .DisplayFor(modelItem => 项。CorvetteItem) </td> <td> @azzedinehtmlsql .DisplayFor(modelItem => 项。CorvetteItemCost) </td> <td> @azzedinehtmlsql .DisplayFor(modelItem => 项。CorvetteStore) </td> <td> @azzedinehtmlsql .DisplayFor(modelItem => 项。修复) </td> <td> @azzedinehtmlsql .DisplayFor(modelItem => 项。升级) </td> <td> @azzedinehtmlsql .DisplayFor(modelItem => 项。日期) </td> <td> @azzedinehtmlsql .DisplayFor(modelItem => 项。维护) </td> <td> @azzedinehtmlsql .DisplayFor(modelItem => 项。OrginalCost) </td> <td> <a asp-action=“编辑” asp-route-id=“@item。CorvetteId“>编辑</a> |<a asp-action=“详细信息” asp-route-id=“@item。CorvetteId“>详细信息</a> |<a asp-action=“删除” asp-route-id=“@item。CorvetteId“>Delete</a> </td> </tr> } </tbody> </table>

Note:此问题总结整理于: System.NullReferenceException: 'Object reference not set to an instance of an object.'

.NET 运行时
.NET 运行时
.NET: 基于 .NET 软件框架的 Microsoft 技术。运行时: 运行未编译为机器语言的应用所需的环境。
38 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Hui Liu-MSFT 40,786 信誉分 Microsoft 供应商
    2024-04-05T08:04:01.7966667+00:00

    此错误通常意味着模型无法从控制器获取值,您应该将值传递给 view

    public IActionResult Index()
            {
                var model= new List<MyModel>()
                {
                    new MyModel{ CorvetteId=1,CorvetteItem="aa"},
                    new MyModel{ CorvetteId=2,CorvetteItem="bb"}
                };
                return View(model);
            }
    
    
    

    如果回复有帮助,请点击“接受答案”并点赞。

    注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

    1 个人认为此答案很有帮助。
    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助