asp.net-mvc 如何使用 ViewBag 显示列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10521831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display a list using ViewBag
提问by user1274646
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:
嗨,我需要使用 viewbag 显示数据列表。但我无法做到。
请帮帮我..
我试过这个东西:
ICollection<Learner> list = new HobbyHomeService().FetchLearner();
ICollection<Person> personlist = new HobbyHomeService().FetchPerson(list);
ViewBag.data = personlist;
and inside view:
和内部视图:
<td>@ViewBag.data.First().FirstName</td>
But this does not show up the value and gives error saying "Model.Person doesnot contain a defibition for First()"
但这并没有显示值并给出错误提示“Model.Person 不包含 First() 的定义”
回答by Leniency
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 is a C# 4 dynamic type. Entities returned from it are also dynamic unless cast. However, extension methods like .First() and all the other Linq ones do not work with dynamics.
ViewBag 是 C# 4 动态类型。除非强制转换,否则从它返回的实体也是动态的。但是,像 .First() 和所有其他 Linq 扩展方法不适用于 dynamics。
Edit - to address the comment:
编辑 - 解决评论:
If you want to display the whole list, it's as simple as this:
如果要显示整个列表,就这么简单:
<ul>
@foreach (var person in ViewBag.data)
{
<li>@person.FirstName</li>
}
</ul>
Extension methods like .First() won't work, but this will.
像 .First() 这样的扩展方法不起作用,但这会起作用。
回答by Chris
To put it all together, this is what it should look like:
总而言之,它应该是这样的:
In the controller:
在控制器中:
List<Fund> fundList = db.Funds.ToList();
ViewBag.Funds = fundList;
Then in the view:
然后在视图中:
@foreach (var item in ViewBag.Funds)
{
<span> @item.FundName </span>
}
回答by pandian_Snkl
simply using Viewbag data as IEnumerable<> list
简单地使用 Viewbag 数据作为 IEnumerable<> 列表
@{
var getlist= ViewBag.Listdata as IEnumerable<myproject.models.listmodel>;
foreach (var item in getlist){ //using foreach
<span>item .name</span>
}
}
//---------or just write name inside the getlist
<span>getlist[0].name</span>
回答by Ad Kahn
i had the same problem and i search and search .. but got no result.
so i put my brain in over drive. and i came up with the below solution.
try this in the View Page
我有同样的问题,我搜索和搜索..但没有结果。
所以我把我的大脑放在了驱动器上。我想出了以下解决方案。在查看页面中试试这个
at the head of the page add this code
在页面的头部添加此代码
@{
var Lst = ViewBag.data as IEnumerable<MyProject.Models.Person>;
}
to display the particular attribute use the below code
使用以下代码显示特定属性
@Lst.FirstOrDefault().FirstName
in your case use below code.
在你的情况下使用下面的代码。
<td>@Lst.FirstOrDefault().FirstName </td>
Hope this helps...
希望这可以帮助...
回答by Md. Tarek Aziz
Use asvariable to cast the Viewbag data to your desired class in view.
使用作为变量的数据Viewbag投射到您想要的类视图。
@{
IEnumerable<WebApplication1.Models.Person> personlist = ViewBag.data as
IEnumerable<WebApplication1.Models.Person>;
// You may need to write WebApplication.Models.Person where WebApplication.Models is
the namespace name where the Person class is defined. It is required so that view
can know about the class Person.
}
In view write this
鉴于写这个
<td>
@(personlist.FirstOrDefault().Name)
</td>
回答by Sahan Pathirana
This is what i did and It worked...
这就是我所做的并且它有效......
C#
C#
ViewBag.DisplaylList = listData;
javascript
javascript
var dispalyList= @Html.Raw(Json.Encode(this.ViewBag.DisplaylList));
for(var i=0;i<dispalyList.length; i++){
var row = dispalyList[i];
..............
..............
}
}
回答by 360Airwalk
Just put a
只要放一个
List<Person>
into the ViewBag and in the View cast it back to List
进入 ViewBag 并在 View 中将其投射回 List
回答by 360Airwalk
//controller You can use this way
public ActionResult Index()
{
List<Fund> fundList = db.Funds.ToList();
ViewBag.Funds = fundList;
return View();
}
<--View ; You can use this way html-->
@foreach (var item in (List<Fund>)ViewBag.Funds)
{
<p>@item.firtname</p>
}
回答by Josue Morales
I had the problem that I wanted to use my ViewBag to send a list of elements through a RenderPartial as the object, and to this you have to do the cast first, I had to cast the ViewBag in the controller and in the View too.
我有一个问题,我想使用我的 ViewBag 通过 RenderPartial 作为对象发送元素列表,为此您必须先进行转换,我还必须在控制器和视图中转换 ViewBag。
In the Controller:
在控制器中:
ViewBag.visitList = (List<CLIENTES_VIP_DB.VISITAS.VISITA>)
visitaRepo.ObtenerLista().Where(m => m.Id_Contacto == id).ToList()
In the View:
在视图中:
List<CLIENTES_VIP_DB.VISITAS.VISITA> VisitaList = (List<CLIENTES_VIP_DB.VISITAS.VISITA>)ViewBag.visitList ;

