[英]Display List in a View MVC
I'm trying to display the list I made in my view but keep getting : "The model item passed into the dictionary is of type 'System.Collections.Generic.List 1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[Standings.Models.Teams]'." 我正在尝试显示我在视图中创建的列表,但不断得到:“传入字典的模型项是'System.Collections.Generic.List 1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1 [Standings.Models.Teams]' 1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
。“
My Controller: 我的控制器:
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);
}
My 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>();
}
My view: 我的看法:
@model IEnumerable<Standings.Models.Teams>
@{
ViewBag.Title = "Standings";
}
@foreach (var item in Model)
{
<div>
@item.Name
<hr />
</div>
}
Any help would be appreciated :) 任何帮助,将不胜感激 :)
Your action method considers model type as List<string>
. 您的操作方法将模型类型视为List<string>
。 But, in your view you are waiting for IEnumerable<Standings.Models.Teams>
. 但是,在您看来,您正在等待IEnumerable<Standings.Models.Teams>
。 You can solve this problem with changing the model in your view to List<string>
. 您可以通过将视图中的模型更改为List<string>
来解决此问题。
But, the best approach would be to return IEnumerable<Standings.Models.Teams>
as a model from your action method. 但是,最好的方法是从操作方法返回IEnumerable<Standings.Models.Teams>
作为模型。 Then you haven't to change model type in your view. 然后,您不必在视图中更改模型类型。
But , in my opinion your models are not correctly implemented. 但是 ,在我看来,你的模型没有正确实现。 I suggest you to change it as: 我建议你改成它:
public class Team
{
public int Position { get; set; }
public string HomeGround {get; set;}
public string NickName {get; set;}
public int Founded { get; set; }
public string Name { get; set; }
}
Then you must change your action method as: 然后,您必须将操作方法更改为:
public ActionResult Index()
{
var model = new List<Team>();
model.Add(new Team { Name = "MU"});
model.Add(new Team { Name = "Chelsea"});
...
return View(model);
}
And, your view: 而且,您的观点:
@model IEnumerable<Standings.Models.Team>
@{
ViewBag.Title = "Standings";
}
@foreach (var item in Model)
{
<div>
@item.Name
<hr />
</div>
}
You are passing wrong mode to you view. 您正在向您查看错误的模式。 Your view is looking for @model IEnumerable<Standings.Models.Teams>
and you are passing var model = tm.Name.ToList();
您的视图正在寻找@model IEnumerable<Standings.Models.Teams>
并且您正在传递var model = tm.Name.ToList();
name list. 名单。 You have to pass list of Teams . 你必须通过团队列表。
You have to pass following model 你必须通过以下模型
var model = new List<Teams>();
model.Add(new Teams { Name = new List<string>(){"Sky","ABC"}});
model.Add(new Teams { Name = new List<string>(){"John","XYZ"} });
return View(model);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.