2

我正在研究 Web Grid HTMl 助手。我想显示数据库中的数据。我成功地显示了数据库中的两列,但不知道如何显示数据库中的图像。我已经制作了 Html 助手来在数据库中显示图像但如何使用在 web 网格 HTML 助手中。所以请帮忙。

4

2 回答 2

0

您可以创建一个返回图像的操作,然后只需在图像标签中使用它的 url:

<img src="/Images/View/123" />

您的操作将类似于:

public ActionResult View(string id)
{
    return base.File("C:\Path\File", "image/jpeg");
}
于 2011-05-10T12:30:37.957 回答
0

I created a small mvc3 app showing the list of Tennis Finalists with a nationality flag next to each player:

For this I used a simple Helper Class:

    using System.Web.Mvc;

    namespace MvcTennis.Helpers
    {
        public static class HtmlHelpers
        {
            public static MvcHtmlString CountryFlag(this HtmlHelper helper, string CountryCode)
            {
                string sRoot = "~/Content/Images/";
                string sFlagPath = sRoot + CountryCode.ToLower() + ".png";
                string image = "<img src=\"" + UrlHelper.GenerateContentUrl(sFlagPath, helper.ViewContext.HttpContext )  + "\" width=\"20px;\" border=\"1\" margin=\"0\" >";
                MvcHtmlString sHtml = new MvcHtmlString(image);
                return sHtml;
            }
        }
    }

The Content/Images folder contains the flag files in .png format.

On the view page (Index.cshtml) we need to add the using directive :

Html/Razor Syntax:

于 2016-03-11T23:08:32.970 回答