In my view I use the following code to get an image from my database (through an action method) and display it in a html tag
<img src="@Url.Action("DownloadBackground", new { projectID = 31 })" />
The Action method named DownloadBackground return a FileContentResult with the image stored in my DB. It works but I would like to create a helper method to replace it.
I would like something like:
@Html.BackgroundFor(31)
Where 31 is the id of the project I need. I would like this helper method to produce the same html code.
So I try to create my helper like this:
public static MvcHtmlString BackgroundFor(this HtmlHelper helper, int id)
{
// What code here ??
var builder = new TagBuilder("a");
builder.MergeAttribute("src", ???);
return new MvcHtmlString(builder.ToString(TagRenderMode.SelfClosing));
}
Try this:
or, you can use
This would do too