c# - Pass data from controller to View - MVC -
i´m tryin make query receive data in database show in view.
but it´s not working. can´t figured out going on because don´t error.
anybody can pls?
thanks in advance
controller:
public actionresult details(int id = 0) { persons p= db.persons.find(id); var query = in db.images 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> }
the linq statement not have evaluated before assigned viewbag
.
try:
controller:
viewbag.imgrev = query.tolist();
view:
@ { var imglist = viewbag.imgrev 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> }
Comments
Post a Comment