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;
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()"
This question is related to
asp.net-mvc
asp.net-mvc-3
model-view-controller
viewbag
This is what i did and It worked...
C#
ViewBag.DisplaylList = listData;
javascript
var dispalyList= @Html.Raw(Json.Encode(this.ViewBag.DisplaylList));
for(var i=0;i<dispalyList.length; i++){
var row = dispalyList[i];
..............
..............
}
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...
Use as variable to cast the Viewbag data to your desired class in view.
@{
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>
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.
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 ;
Just put a
List<Person>
into the ViewBag and in the View cast it back to List
//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>
}
simply using Viewbag data as IEnumerable<> list
@{
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>
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>
}
Source: Stackoverflow.com