从数据库中检索图像并显示在表中

时间:2015-01-08 11:14:12

标签: c# asp.net-mvc image

我需要做这样的事情。

  

enter image description here

这些是商店接受的付款类型。如何显示每种付款类型的相应图像。图像来自数据库,要显示的付款类型基于商店中可用的付款类型。我尝试使用switch case,这是结果

  

enter image description here

这是我的代码:

_DeliveryDetailsPartial

<table class="table table-noborder col-md-3" style="margin-top: 10px;">
    <tr>
        <th style="text-transform:uppercase; padding:5px; font-weight:bold" colspan=" 4">Payments Accepted</th>
    </tr>
    @foreach (ReferenceAcceptedPayment payment in Model.Item4)
    {
        <tr style="text-align: center">
            switch (payment.PaymentID)
            {
            case 1:
                <td colspan="1"><img src="~/Content/Images/amex.png"></td>
                break;

            case 2:
                <td colspan="1"><img src="~/Content/Images/discover.png"></td>
                break;

            case 3:
                <td colspan="1"><img src="~/Content/Images/mastercard.png"></td>
                break;

            case 4:
                <td colspan="1"><img src="~/Content/Images/visa.png"></td>
                break;

            case 5:
                <td colspan="1"><img src="~/Content/Images/giftcard.png"></td>
                break;

            case 6:
                <td colspan="1"><img src="~/Content/Images/cash.png"></td>
                break;

            default:
                break;
            }
        </tr>
    }
</table>

DeliveryDetailController

    public ActionResult NewAddress(string id)
    {
        GetStoreDetails();
        return View();
    }

GetStoreDetails

    public ActionResult GetStoreDetails(string id)
    {
        var store = new Tuple<List<Store>, List<StoreHour>, List<StoreHour>, List<ReferenceAcceptedPayment>>
        (_repo.GetStore(id), _repo.GetStoreHourByDay(dayOftheWeek), _repo.GetStoreHourByID(int.Parse(id)), _repo.GetStoreAcceptedPayment(int.Parse(id)));

        return View(store);
    }

1 个答案:

答案 0 :(得分:0)

您需要创建一个Action方法,返回图像从View中调用Controller的Action(DislpayImage()),如下所示:

<img src="@Url.Action("DislpayImage", "Controller",new { id=Model.Id })" alt="myimage" />

只需将要显示的图像的ID传递给Controller操作

public class ImageController : Controller
{
    public ActionResult DislpayImage( int id )
    {
        var imageData = ...get bytes from database...

        return File( imageData, "image/jpg" );
    }
}

现在传递要在视图中显示的图像的ID,如下所示:

<img src="@Url.Action("DislpayImage", "Controller", new { id="2" })" alt="myimage" />

现在,您将获得ID为2的图像。