[asp.net-mvc] How to display a list using ViewBag

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()"

The answer is


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>
}

Examples related to asp.net-mvc

Using Lato fonts in my css (@font-face) Better solution without exluding fields from Binding Vue.js get selected option on @change You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to send json data in POST request using C# VS 2017 Metadata file '.dll could not be found The default XML namespace of the project must be the MSBuild XML namespace How to create roles in ASP.NET Core and assign them to users? The model item passed into the dictionary is of type .. but this dictionary requires a model item of type How to use npm with ASP.NET Core

Examples related to asp.net-mvc-3

Better solution without exluding fields from Binding IIs Error: Application Codebehind=“Global.asax.cs” Inherits=“nadeem.MvcApplication” Can we pass model as a parameter in RedirectToAction? return error message with actionResult Why is this error, 'Sequence contains no elements', happening? Failed to load resource: the server responded with a status of 500 (Internal Server Error) in Bind function 500.19 - Internal Server Error - The requested page cannot be accessed because the related configuration data for the page is invalid String MinLength and MaxLength validation don't work (asp.net mvc) How to set the value of a hidden field from a controller in mvc How to set a CheckBox by default Checked in ASP.Net MVC

Examples related to model-view-controller

Vue JS mounted() Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported for @RequestBody MultiValueMap Display List in a View MVC What exactly is the difference between Web API and REST API in MVC? No default constructor found; nested exception is java.lang.NoSuchMethodException with Spring MVC? Spring MVC Missing URI template variable What is difference between MVC, MVP & MVVM design pattern in terms of coding c# Add directives from directive in AngularJS No mapping found for HTTP request with URI Spring MVC Limiting number of displayed results when using ngRepeat

Examples related to viewbag

Updating PartialView mvc 4 How to use a ViewBag to create a dropdownlist? How ViewBag in ASP.NET MVC works How to create own dynamic type or dynamic object in C#? Pass a simple string from controller to a view MVC3 How to display a list using ViewBag How do I access ViewBag from JS The name 'ViewBag' does not exist in the current context What's the difference between ViewData and ViewBag?