Helpers.cs
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace mvc.Helpers
{
public static class ImageHelpers
{
public static IHtmlString Image(this HtmlHelper helper, string id, string url, string alternateText)
{
return Image(helper, id, url, alternateText, null);
}
public static IHtmlString Image(this HtmlHelper helper, string id, string url, string alternateText, object htmlAttributes)
{
var builder = new TagBuilder("img");
builder.GenerateId(id);
builder.MergeAttribute("src", url);
builder.MergeAttribute("alt", alternateText);
builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
return new MvcHtmlString(builder.ToString(TagRenderMode.SelfClosing));
}
}
}
index.cshtml
@{
ViewBag.Title = "Home Page";
}
@using mvc.Helpers;
@Html.Image("img1", "~/Content/XBox.jpg", "XBox Console")