How can I use the linq join function result inserted in a viewbag from razor view?
In following code I am try set list of Photo in ViewBag.photos
to using it in ViewPage
Razor page.
var result = photos.Join(projects, (pho => pho.ProjectID), (pro => pro.ProjectID), ((pho, pro) => new { photos = pho })).ToList();
ViewBag.photos = result;
When I try used the ViewBag.photos
in view page, give me some error.
<div class="col-lg-3 col-sm-6 col-md-6" data-aos="fade-up" data-aos-delay="60">
@foreach (var item in (IEnumerable<Wenar.Models.DomainModel.Photo>) ViewBag.photos)
{
<img src="~/ProjectPhotos/@item.Photo1;" />
}
</div>
That error mention is:
$exception {"Unable to cast object of type 'System.Collections.Generic.List
1[<>f__AnonymousType6
1[Wenar.Models.DomainModel.Photo]]' to type 'System.Collections.Generic.IEnumerable`1[Wenar.Models.DomainModel.Photo]'."} System.InvalidCastException
You need to create list of objects type of Wenar.Models.DomainModel.Photo
, you can use Select
method:
... ((pho, pro) => new { photos = pho }))
.Select(s => new Wenar.Models.DomainModel.Photo{ photoId = s.photoId, photoName = s.photoName })
.ToList();