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>
}