2

I am working on Web Grid HTMl helper.I want to display data from database.I succeed in displaying two columns from database but dont know hoe to display image from database.I have made the Html helper to display image in database but how to use in web grid HTML helper. so pls help.

Ravneet
  • 21
  • 3

2 Answers2

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:

0

You could create an action that returns an image, then just use the url to it in your image tag:

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

Your action would be something like:

public ActionResult View(string id)
{
    return base.File("C:\Path\File", "image/jpeg");
}
Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275