检索图像以查看 ASP.MVC
Posted
技术标签:
【中文标题】检索图像以查看 ASP.MVC【英文标题】:Retriving Image to View ASP.MVC 【发布时间】:2021-11-08 13:20:33 【问题描述】:我已将附件(图像)保存到我的 asp.MVC 应用程序的数据库中。现在我想在视图部分返回图像。但似乎检索图像动作需要按预期触发。需要大家帮忙。这是我的观点
if (Model.PurchasingItemsList.Count != 0)
<div>
<table class="table">
<tr>
<th>
Supplier Name
</th>
<th>
Item Description
</th>
<th>
Unit Amount
</th>
<th>
Requesting Qty
</th>
<th>
Recomendation
</th>
<th>
Attachment
</th>
<th></th>
</tr>
@foreach (var item in Model.PurchasingItemsList)
<tr>
<td>
@Suppliers.Find(x => x.Value == item.Supp_Id.ToString()).Text
</td>
<td>
@itemsDetails.Find(x => x.Value == item.Itm_Description_Id.ToString()).Text
</td>
<td>
@html.DisplayFor(modelItem => item.Unit_Amount)
</td>
<td>
@Html.DisplayFor(modelItem => item.Qty)
</td>
<td>
@Html.DisplayFor(modelItem => item.Recomandation)
</td>
<td>
<img src="/Content/RetrieveImage/@item.Id" height=50 width=50 />
</td>
</tr>
</table>
</div>
这是我的控制器视图操作和图像检索操作。
public ActionResult View(int? id)
List<M_Company> CompanyList = db.CreateCompany.Where(x => x.Status == true).ToList();
List<SelectListItem> CompanyDropDown = CompanyList.Select(x => new SelectListItem Text = x.CompanyName, Value = x.Id.ToString() ).ToList();
List<M_Location> LocationList = db.Master_Locations.Where(x => x.Status == true).ToList();
List<SelectListItem> LocationsDropd = LocationList.Select(x => new SelectListItem Text = x.Location, Value = x.Id.ToString() ).ToList();
List<M_Employee> EmpList = db.CreateEmployee.Where(x => x.Status == true).ToList();
List<SelectListItem> EmpDropDown = EmpList.Select(x => new SelectListItem Text = x.EmpName, Value = x.Id.ToString() ).ToList();
List<M_Supplier> SupList = db.M_Supplier.Where(x => x.Status == true).ToList();
List<SelectListItem> SupDropDown = SupList.Select(x => new SelectListItem Text = x.SuppName, Value = x.Id.ToString() ).ToList();
List<M_ItemsForQuotation> DescList = db.M_ItemsForQuotation.Where(x => x.status == true).ToList();
List<SelectListItem> DescListDropDown = DescList.Select(x => new SelectListItem Text = x.Itm_Desc, Value = x.Id.ToString() ).ToList();
List<M_VehicleTypes> VehiTypList = db.Master_VehicleTypes.Where(v => v.Status == true).ToList();
List<SelectListItem> VTypeDropDown = VehiTypList.Select(v => new SelectListItem Text = v.VehiType, Value = v.Id.ToString() ).ToList();
List<Request_Types> RequestTyleList = db.Request_Types.Where(r => r.Status == true).ToList();
List<SelectListItem> ReqTypeDropDown = RequestTyleList.Select(r => new SelectListItem Text = r.Request_Type, Value = r.Id.ToString() ).ToList();
TempData["EmployeeList"] = EmpDropDown;
TempData["SupplierList"] = SupDropDown;
TempData["Itm_DesList"] = DescListDropDown;
TempData["ComapnyList"] = CompanyDropDown;
TempData["LocationList"] = LocationsDropd;
TempData["VehiTypList"] = VTypeDropDown;
TempData["RequestTyleList"] = ReqTypeDropDown;
if (id == null)
return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
AppRequest appRequest = db.AppRequest.Find(id);
if (appRequest.Purchase.Count != 0)
appRequest.PurchasingEmpList = appRequest.Purchase != null ? appRequest.Purchase.First().PurchasingEmpl.ToList() : null;
appRequest.PurchasingItemsList = appRequest.Purchase != null ? appRequest.Purchase.First().PurchasingItems.ToList() : null;
else if (appRequest.General.Count != 0)
appRequest.GeneralItmsList = appRequest.General != null ? appRequest.General.First().GeneralItms.ToList() : null;
else if (appRequest.Suspense.Count != 0)
appRequest.SuspenseDetailsList = appRequest.Suspense != null ? appRequest.Suspense.First().SuspenseDetails.ToList() : null;
if (appRequest == null)
return HttpNotFound();
return View(appRequest);
public ActionResult RetrieveImage(int id)
var q = from temp in db.PurchasingItems where temp.Id == id select temp.Attachment;
byte[] cover = q.First();
if (cover != null)
return File(cover, "image/jpg");
else
return null;
当涉及到视图时,附件字段没有什么可显示的。我在动作检索图像上设置了一个断点,它不会被触发。我能得到一些帮助吗?
【问题讨论】:
【参考方案1】:我认为你的 HTML 应该是:
<img src="@Url.Action("RetrieveImage", new id = item.Id )" />
【讨论】:
以上是关于检索图像以查看 ASP.MVC的主要内容,如果未能解决你的问题,请参考以下文章
ASP MVC 2 问题 - 尝试在视图中的 for 循环中显示图像
从 ASP.net Core 2 MVC 应用程序中的 appSettings.json 存储/检索 ConnectionString