如何显示使用ViewBag列表 [英] How to display a list using ViewBag

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

问题描述

我需要使用viewbag.but我不能够做到这一点,以显示数据列表。结果
请帮帮我..
结果我想这件事情:

Hi i need to show a list of data using viewbag.but i am not able to do it.
Please Help me..
I tried this thing:

 ICollection<Learner> list = new HobbyHomeService().FetchLearner();
 ICollection<Person> personlist = new HobbyHomeService().FetchPerson(list);
 ViewBag.data = personlist;

和里面的观点:

 <td>@ViewBag.data.First().FirstName</td>

但是,这并不表明在价值,并给出错误说Model.Person亘古不包含第一()一defibition

But this does not show up the value and gives error saying "Model.Person doesnot contain a defibition for First()"

推荐答案

在你看来,你必须将它转换回原始类型。如果不投,它只是一个对象。

In your view, you have to cast it back to the original type. Without the cast, it's just an object.

<td>@((ViewBag.data as ICollection<Person>).First().FirstName)</td>

ViewBag是一个C#4动态类型。从它返回的实体也是动态的,除非施法。然而,扩展方法。首先一样()和所有其他LINQ的那些<一个href=\"http://stackoverflow.com/questions/258988/will-the-dynamic-keyword-in-c4-support-extension-methods\">do不能与动态工作。

编辑 - 解决意见:

如果你想显示整个列表,它是像这样简单:

If you want to display the whole list, it's as simple as this:

foreach (var person in ViewBag.data)
{
    <li>@person.FirstName</li>
}

扩展方法。首先像()将无法正常工作,但这种意愿。

Extension methods like .First() won't work, but this will.

这篇关于如何显示使用ViewBag列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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