Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.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
C# 尝试在视图中迭代列表时出现错误消息_C#_Asp.net Mvc - Fatal编程技术网

C# 尝试在视图中迭代列表时出现错误消息

C# 尝试在视图中迭代列表时出现错误消息,c#,asp.net-mvc,C#,Asp.net Mvc,在我的模型中,我有以下字符串列表: public static List<string> listOfNames = new List<string>(); 在视图的顶部,我看到: @model IEnumerable<Exercise_3.Models.NameModel> @model IEnumerable 我想这是错误的,但我不知道该用什么来代替。当我有一个对象列表时,这一行从一开始就没问题,但现在我只想使用一个字符串列表 运行应用程序时收到的错

在我的模型中,我有以下字符串列表:

public static List<string> listOfNames = new List<string>();
在视图的顶部,我看到:

@model IEnumerable<Exercise_3.Models.NameModel>
@model IEnumerable
我想这是错误的,但我不知道该用什么来代替。当我有一个对象列表时,这一行从一开始就没问题,但现在我只想使用一个字符串列表

运行应用程序时收到的错误消息如下:

传递到字典的模型项的类型为“System.Collections.Generic.List`1[System.String]”,但此字典需要类型为“System.Collections.Generic.IEnumerable`1[Exercise_3.Models.NameModel]”的模型项


发送到视图的模型是
NameModel
listOfNames
属性值。
listOfNames
属性的类型为
List
。但是在razor视图中,您将模型类型指定为
IEnumerable
。这些不匹配

您可以更新razor视图以使用
列表
作为模型类型

@model List<string>
<h1>Items</h1>
@foreach(var item in Model)
{
  <h2>@item</h2>
}
@型号列表
项目
@foreach(模型中的var项目)
{
@项目
}

能否尝试将
@model-IEnumerable
更改为
@model-IEnumerable
?您传递到视图的模型是
列表
,因此在视图中它需要是
@model-IEnumerable
,谢谢你们两位的帮助!
@model List<string>
<h1>Items</h1>
@foreach(var item in Model)
{
  <h2>@item</h2>
}