Этот вызов прослушивает меня уже несколько дней. Я, очевидно, что-то делаю неправильно, но мне хотелось бы знать, что.Как отобразить изображение из базы данных с помощью <PagedList> в MVC 4 EF 6
Как я могу отображать свои фотографии в своем представлении индекса при использовании класса PagedList?
Я не могу получить изображение для правильного отображения при использовании класса PagedList. Все прекрасно работает в моем Edit View при использовании @model HeHu.Models.Candidate
, но при попытке показать фотографии в моем Index View, который с помощью PagedList @model PagedList.IPagedList<HeHu.Models.Candidate>
, поле Model.PictFiles не распознается как в следующем коде:
@if (Model.PictFiles.Any(.....)
<div class="col-md-10">
<img width="150" src="~/[email protected](.....)
</div>
</div>
}
Picture of red-underlined code
То, что я хочу добиться: записи кандидатов в ряд, в том числе картины.
модели являются следующие:
кандидат:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace HeHu.Models
{
public class Candidate
{
public int CandidateID { get; set; }
public int CsearchID { get; set; }
[StringLength (50)]
public string CandLastName { get; set;
public string CandFirstName { get; set; }
public string CandCompany { get; set; }
[Timestamp]
public byte[] RowVersion { get; set; }
public virtual Csearch Csearch { get; set; }
public virtual ICollection<CandAction> CandAction { get; set; }
public virtual ICollection<PictFile> PictFiles { get; set;}
public virtual ICollection<FilePath> FilePaths { get; set; }
}
}
Pictfile:
using System.ComponentModel.DataAnnotations;
namespace HeHu.Models
{
public class PictFile
{
public int PictFileId { get; set; }
[StringLength(255)]
public string PictFileName { get; set; }
[StringLength(100)]
public string ContentType { get; set; }
public byte[] Content { get; set; }
public FileType FileType { get; set; }
public int CandidateId { get; set; }
public virtual Candidate Candidate { get; set; }
}
}
Контроллер для индекса (упрощенно) следующим образом:
public class CandidateController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
public ActionResult Index(string sortOrder, string currentfilter, string searchString, int? opdrachtNummer, int? id, int? page)
{
//Adding the Pager
if (searchString != null || opdrachtNummer.HasValue)
{
page = 1;
}
else
{
searchString = currentfilter;
}
ViewBag.Currentfilter = searchString;
var candidates = from c in db.Candidates
.Include(c => c.PictFiles)
.Include(c => c.FilePaths)
select c;
int pageSize = 10;
int pageNumber = (page ?? 1);
return View(candidates.ToPagedList(pageNumber, pageSize));
}
И t он контроллер для изображения:
using HeHu.DAL;
using HeHu.Models;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace HeHu.Controllers
{
public class PictFileController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: PictFile
public ActionResult Index(int id)
{
var pictFileToRetrieve = db.PictFiles.Find(id);
return File(pictFileToRetrieve.Content, pictFileToRetrieve.ContentType);
}
}
}
Наконец, View выглядит следующим образом:
@model PagedList.IPagedList<HeHu.Models.Candidate>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
ViewBag.Title = "Candidates";
}
<h2><cr/>Kandidaten</h2>
@using (Html.BeginForm("Index", "Candidate", null, FormMethod.Get, new { enctype = "multipart/form-data" }))
{
<table class="table">
<tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.CandLastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.CandFirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.CandCompany)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.CandidateID }) |
@Html.ActionLink("Details", "Details", new { id = item.CandidateID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.CandidateID })
</td>
</tr>
}
</table>
}
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))