【发布时间】:2015-05-11 13:26:55
【问题描述】:
我正在尝试显示我在视图中创建的列表,但不断收到:“传递到字典中的模型项的类型为 'System.Collections.Generic.List1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[Standings.Models.Teams] '。”
我的控制器:
public class HomeController : Controller
{
Teams tm = new Teams();
public ActionResult Index()
{
var model = tm.Name.ToList();
model.Add("Manchester United");
model.Add("Chelsea");
model.Add("Manchester City");
model.Add("Arsenal");
model.Add("Liverpool");
model.Add("Tottenham");
return View(model);
}
我的模特:
public class Teams
{
public int Position { get; set; }
public string HomeGround {get; set;}
public string NickName {get; set;}
public int Founded { get; set; }
public List<string> Name = new List<string>();
}
我的看法:
@model IEnumerable<Standings.Models.Teams>
@{
ViewBag.Title = "Standings";
}
@foreach (var item in Model)
{
<div>
@item.Name
<hr />
</div>
}
任何帮助将不胜感激:)
【问题讨论】:
-
您要传递团队集合还是团队名称列表(这是您要传递的)?
-
为什么一个足球队对象命名为
Teams(复数),为什么一个球队有多个名字?我认为您需要仔细查看您的模型! -
在下面试试我的答案。您的模型未正确实施。
标签: c# asp.net-mvc list model-view-controller