C# 尝试在网页中加载.json时出错:对象引用未设置为对象的实例
我试图在网页中显示json文件中的数据,但出现了此错误。C# 尝试在网页中加载.json时出错:对象引用未设置为对象的实例,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,我试图在网页中显示json文件中的数据,但出现了此错误。 这是my test.cshtml,错误在@foreach行: @model API_STA_1.Classes.Description @{ ViewData["description"] = "Index"; } <h2>Index</h2> <p> <a asp-action="Create">Create New</a> </p> <
这是my test.cshtml,错误在
@foreach
行:
@model API_STA_1.Classes.Description
@{
ViewData["description"] = "Index";
}
<h2>Index</h2>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.projects)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.ActionLink("Update", "Update", new { /*id = item.PrimaryKey*/}) |
@Html.ActionLink("Delete", "Delete", new { /*id = item.PrimaryKey*/}) |
</td>
</tr>
}
</tbody>
</table>
@model API\u STA\u 1.Classes.Description
@{
ViewData[“描述”]=“索引”;
}
指数
您没有从控制器操作向视图传递模型对象
您需要将其传递回视图
见:
您没有从控制器操作向视图传递模型对象
您需要将其传递回视图
见:
您需要显示控制器操作code@EhsanSajjad这个?您需要显示控制器操作code@EhsanSajjad这个?因此,我将其更改为description
,但现在出现了一个错误,因为它正在视图文件夹中查找test.cshtml,但我没有视图文件夹@然后试试你已经做过的:返回视图(“test/test.cshtml”,description)代码>有效!非常感谢。我是新手,这帮了我大忙@T.v.A.没问题,欢迎,答案中的代码应该有效,但启动时可能缺少某些内容,因此我们需要显式指定视图路径,否则它会按约定工作,因此我将其更改为description
,但现在我收到一个错误,因为它正在视图文件夹中查找test.cshtml,但我没有视图文件夹@然后试试你已经做过的:返回视图(“test/test.cshtml”,description)代码>有效!非常感谢。我是新手,这帮了我大忙@T.v.A.没问题,欢迎使用,答案中的代码应该可以工作,但启动时可能缺少一些东西,因此我们需要明确指定视图路径,否则它会按照约定工作
var description = JsonConvert.DeserializeObject<List<Description>>(json);
return View(description);
return View("test/test.cshtml", description);