在查看MVC显示列表 [英] Display List in a View MVC

查看:170
本文介绍了在查看MVC显示列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我显示在我看来上榜,但不断收到:传递到字典的模型项的类型是System.Collections.Generic.List 1 [System.String ]',但本词典需要类型的模型项目System.Collections.Generic.IEnumerable 1 [Standings.Models.Teams]'。



我的控制器:

 公共类HomeController的:控制器
{
队TM =新车队( );

公众的ActionResult指数()
{
VAR模型= tm.Name.ToList();

model.Add(曼联);
model.Add(切尔西);
model.Add(曼城);
model.Add(阿森纳);
model.Add(利物浦);
model.Add(热刺);

返回查看(模型);
}



我的模型:

 公共一流强队
{
公众诠释位置{搞定;组; }
公共字符串HomeGround {搞定;设置;}
公共字符串昵称{搞定;设置;}
公众诠释成立{搞定;组; }

公开名单<串GT;名称=新的List<串GT;();
}



我的观点:

  @model IEnumerable的< Standings.Models.Teams> 

@ {
ViewBag.Title =排名;
}

@foreach(以型号VAR项)
{
< DIV>
@ item.Name
<小时/>
< / DIV>
}



任何帮助,将不胜感激:)


< DIV CLASS =h2_lin>解决方案

您的行动方法考虑机型为列表<串GT; 。但是,在你看来,你正在等待的IEnumerable< Standings.Models.Teams>
你可以更改您的视图模型解决这个问题列表与LT;串>



不过,最好的办法是返回的IEnumerable< Standings.Models.Teams> 从模型的操作方法。那么你有没有改变机型在您的视图。



但是,在我看来,你的模型不正确实施。我建议你去改变它为:

 公共类团队
{
公众诠释位置{搞定;组; }
公共字符串HomeGround {搞定;设置;}
公共字符串昵称{搞定;设置;}
公众诠释成立{搞定;组; }
公共字符串名称{;组; }
}



然后,你必须改变你的操作方法为:

 公众的ActionResult指数()
{
VAR模型=新的List<团队及GT;();

model.Add(新团队{名称=MU});
model.Add(新团队{名称=切尔西});


返回查看(模型);
}

和,你的看法:

  @model IEnumerable的< Standings.Models.Team> 

@ {
ViewBag.Title =排名;
}

@foreach(以型号VAR项)
{
< DIV>
@ item.Name
<小时/>
< / 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.IEnumerable1[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显示列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆