1

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

1 回答 1

1

linq 语句在分配给ViewBag.

尝试:

控制器:

ViewBag.ImgRev = query.ToList();

看法:

@ {
    var imgList = ViewBag.ImgRev as IEnumerable<string>;
}

@foreach (var p in imgList)
{ 
    <div><img src="~/Files/" + @p /></div>
}

使用 ViewModel 的替代方案:

public class SomePageViewModel
{
    public Persons Persons { get; set; }
    public IEnumerable<string> Files { get; set; }
}

控制器:

...
var model = new SomePageViewModel { Persons = p, Files = query.ToList() };
return model;

看法:

@foreach (var file in Model.Files)
{ 
    <div><img src="~/Files/" + @file /></div>
}
于 2013-09-03T15:47:09.560 回答