1) Create extension method
Here inside my helper class I did the following coding
using System.Web.Mvc;
...
...
public static class MyHelpers
{
public static MvcHtmlString Image(this HtmlHelper helper, string src, string alterText)
{
var builder = new TagBuilder("img");
builder.MergeAttribute("src", src);
builder.MergeAttribute("alt", alterText);
return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
}
}
2) Add the using directive in the View
The helper class resides within the "MvcBookReview.Infrastructure" namespace. Hence:
@using MvcBookReview.Infrastructure
3) Call the custom helper in view
<div>
@Html.Image(@Model.ImageUrl, @Model.AlternateText)
</div>
Result:
Here inside my helper class I did the following coding
using System.Web.Mvc;
...
...
public static class MyHelpers
{
public static MvcHtmlString Image(this HtmlHelper helper, string src, string alterText)
{
var builder = new TagBuilder("img");
builder.MergeAttribute("src", src);
builder.MergeAttribute("alt", alterText);
return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
}
}
2) Add the using directive in the View
The helper class resides within the "MvcBookReview.Infrastructure" namespace. Hence:
@using MvcBookReview.Infrastructure
3) Call the custom helper in view
<div>
@Html.Image(@Model.ImageUrl, @Model.AlternateText)
</div>
Result:
No comments:
Post a Comment