在我的模型中,我有这个字符串列表:
public static List<string> listOfNames = new List<string>();
在Controller中我有这个动作:
public ActionResult NameInput()
{
NameModel.prepareList();
return View("NameInput", NameModel.listOfNames);
}
在视图的顶部我有这个:
@model IEnumerable<Exercise_3.Models.NameModel>
我想这是错误的地方,但我不知道该怎么用。当我有一个对象列表时,这条线从一开始就没问题,但现在我只想使用一个字符串列表。
运行应用程序时收到的错误消息如下:
传递到字典中的模型项的类型是&#39; System.Collections.Generic.List`1 [System.String]&#39;,但是这个字典需要一个类型为&#39; System的模型项.Collections.Generic.IEnumerable`1 [Exercise_3.Models.NameModel]&#39;
答案 0 :(得分:0)
您要发送到视图的模型是listOfNames
属性值NameModel
。 listOfNames
属性的类型为List<string>
。但是在您的剃刀视图中,您将模型类型指定为IEnumerable<Exercise_3.Models.NameModel>
。那些不匹配。
您可以更新剃刀视图以使用List<string>
作为模型类型
@model List<string>
<h1>Items</h1>
@foreach(var item in Model)
{
<h2>@item</h2>
}