Reputation: 4964
I´m tryin to make a query to receive all data in my database to show it in my view.
but it´s not working. i can´t figured it out what is going on because i don´t get any error.
anybody can help with this pls?
thanks in advance
Controller:
public ActionResult Details(int id = 0)
{
Persons p= db.persons.Find(id);
var query = from a in db.images
where a.Id_Person== id
select a.FileName;
ViewBag.ImgRev= query;
if (mag == null)
{
return HttpNotFound();
}
return View(p);
}
View:
@foreach (var p in ViewBag.ImgRev)
{
<div>
<img src="~/Files/" + @p /></div>
}
Upvotes: 1
Views: 1713
Reputation: 21245
The linq statement will not have evaluated before it is assigned to the ViewBag
.
Try:
Controller:
ViewBag.ImgRev = query.ToList();
View:
@ {
var imgList = ViewBag.ImgRev as IEnumerable<string>;
}
@foreach (var p in imgList)
{
<div><img src="~/Files/" + @p /></div>
}
Alternative using ViewModel:
public class SomePageViewModel
{
public Persons Persons { get; set; }
public IEnumerable<string> Files { get; set; }
}
Controller:
...
var model = new SomePageViewModel { Persons = p, Files = query.ToList() };
return model;
View:
@foreach (var file in Model.Files)
{
<div><img src="~/Files/" + @file /></div>
}
Upvotes: 1