如何使用显示数据库中的图像在MVC 4 EF 6
本文关键字:图像 MVC EF 何使用 PagedList 数据库 显示 | 更新日期: 2023-09-27 18:18:17
这个挑战已经困扰我好几天了。我显然做错了什么事,但我希望我知道是什么。
我怎么能显示我的图片在我的索引视图时使用PagedList类?
当使用PagedList类时,我无法使图片正确显示。当使用@model HeHu.Models.Candidate
时,所有工作都很好,在我的编辑视图中,但是当试图在我的索引视图中显示使用PagedList @model PagedList.IPagedList<HeHu.Models.Candidate>
的图片时,字段模型。以下代码无法识别PictFiles:
@if (Model.PictFiles.Any(.....)
<div class="col-md-10">
<img width="150" src="~/PictFile?id=@Model.PictFiles.First(.....)
</div>
</div>
}
红色下划线代码图片
我想达到的目标:每行显示候选人的记录,包括图片。
模型如下:
候选人:
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));
}
和图像的控制器:
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);
}
}
}
最后,视图看起来像这样:
@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 }))
在编辑视图中,它的正常工作方式是使用帮助器:
@if (Model.PictFiles.Any(.....)
<div class="col-md-10">
<img width="150" src="~/PictFile?id=@Model.PictFiles.First(.....)
</div>
</div>
}
这里的Model指的是Candidate类型。但是,在Index视图中,您有一个候选对象的页面列表,因此为了使用helper来显示给定候选对象的图片,您应该使用正在迭代的集合中的项:
添加一个td来显示候选人的照片:
<td>
@if (item.PictFiles.Any(x => x.FileType == FileType.PersonalPicture))
{
<div class="col-md-10">
<img width="150" src="~/PictFile?id=@(item.PictFiles.First(x => x.FileType == FileType.PersonalPicture).PictFileId)"/>
</div>
}
</td>
但是,请注意,这次您不能请求Model。PictFiles作为模型不是一个Candidate
,而是一个PagedList
。此外,当您在foreach循环中遍历Model时,您可以根据需要使用foreach变量item:
@foreach (var item in Model)
{
<tr>
<td>
@if (item.PictFiles.Any(x => x.FileType == FileType.PersonalPicture))
{
<div class="col-md-10">
<img width="150" src="~/PictFile?id=@(item.PictFiles.First(x => x.FileType == FileType.PersonalPicture).PictFileId)"/>
</div>
}
</td>
<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>
}
希望这对你有帮助!
感谢Karel的正确解释和正确答案!效果很好。最终页面生成如下所示。我觉得有趣的是src="/PictFiles?id=1"
。它看起来好像将文件存储在目录中,而不是在数据库中。
<td>
<div class="col-md-10">
<img width="150" src="/PictFile?id=1" />
</div>
</td>