检索图像以查看 ASP.MVC
Retriving Image to View ASP.MVC
我已将附件(图像)保存到我的 asp.MVC 应用程序的数据库中。现在我想 return 视图部分中的图像。但似乎检索图像动作需要按预期触发。需要大家的帮助。这是我的观点
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" alt="" 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;
}
}
当涉及到视图时,附件字段没有显示。我在动作检索图像上设置了一个断点,它不会触发。我能得到一些帮助吗?
我认为你的HTML应该是:
<img src="@Url.Action("RetrieveImage", new { id = item.Id })" />
我已将附件(图像)保存到我的 asp.MVC 应用程序的数据库中。现在我想 return 视图部分中的图像。但似乎检索图像动作需要按预期触发。需要大家的帮助。这是我的观点
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" alt="" 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;
}
}
当涉及到视图时,附件字段没有显示。我在动作检索图像上设置了一个断点,它不会触发。我能得到一些帮助吗?
我认为你的HTML应该是:
<img src="@Url.Action("RetrieveImage", new { id = item.Id })" />