本文介绍了在视图 MVC 中显示列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试显示我在视图中创建的列表,但不断收到:传递到字典中的模型项的类型为‘System.Collections.Generic.List1[System.String]’,但是这个字典需要一个'System.Collections.Generic.IEnumerable
1[Standings.Models.Teams]'类型的模型项."
我的控制器:
公共类 HomeController : 控制器{团队 tm = 新团队();公共 ActionResult 索引(){var 模型 = tm.Name.ToList();model.Add("曼联");模型.Add("切尔西");model.Add("曼彻斯特城");模型.Add("阿森纳");模型.Add("利物浦");模型.Add("托特纳姆");返回视图(模型);}
我的模型:
公开课团队{公共 int 位置 { 得到;放;}公共字符串 HomeGround {get;放;}公共字符串昵称{get;放;}公共 int 成立 { 得到;放;}公共列表Name = new List();}
我的观点:
@model IEnumerable@{ViewBag.Title = "排名";}@foreach(模型中的变量项目){<div>@项目名称<小时/>
}
任何帮助将不胜感激:)
解决方案
您的操作方法将模型类型视为List
.但是,在您看来,您正在等待 IEnumerable
.您可以通过将视图中的模型更改为 List
来解决此问题.
但是,最好的方法是将 IEnumerable
作为来自您的操作方法的模型返回.那么您不必在视图中更改模型类型.
但是,在我看来,您的模型没有正确实现.我建议你把它改成:
公开课团队{公共 int 位置 { 得到;放;}公共字符串 HomeGround {get;放;}公共字符串昵称{get;放;}公共 int 成立 { 得到;放;}公共字符串名称 { 获取;放;}}
那么您必须将您的操作方法更改为:
public ActionResult Index(){var model = new List();model.Add(new Team { Name = "MU"});model.Add(new Team { Name = "Chelsea"});...返回视图(模型);}
而且,您的观点:
@model IEnumerable@{ViewBag.Title = "排名";}@foreach(模型中的变量项目){<div>@项目名称<小时/>
}
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.List1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[Standings.Models.Teams]'."
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 asList<string>
. But, in your view you are waiting for IEnumerable<Standings.Models.Teams>
.You can solve this problem with changing the model in your view to List<string>
.
But, the best approach would be to return IEnumerable<Standings.Models.Teams>
as a model from your action method. 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>
}
这篇关于在视图 MVC 中显示列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!