code_junkie

Where is Html.Image in ASP .NET MVC RC

Nov 14th, 2011
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. public static class HtmlHelperExtensions
  2. {
  3.  
  4. public static string Image( this HtmlHelper helper,
  5. string url,
  6. string altText,
  7. object htmlAttributes )
  8. {
  9. TagBuilder builder = new TagBuilder( "img" );
  10. builder.Attributes.Add( "src", url );
  11. builder.Attributes.Add( "alt", altText );
  12. builder.MergeAttributes( new RouteValueDictionary( htmlAttributes ) );
  13. return builder.ToString( TagRenderMode.SelfClosing );
  14. }
  15. }
  16.  
  17. <%= Html.Image( Url.Content( "~/Content/images/img.png" ),
  18. "alt text",
  19. new { id = "myImage", border = "0" } )
  20. %>
  21.  
  22. <img src="<%= Url.Content( "~/content/..." ) %>"
  23. alt="alt text"
  24. class="has-border" />
  25.  
  26. <img src="" alt="" />
  27.  
  28. <img src="<%= ViewData["ImageSource"] %>" alt="" />
  29.  
  30. <img src="<%= ViewData.Model.ImageSource %>" alt="" />
  31.  
  32. <img src="<%= ResolveUrl(ViewData.Model.ImageSource) %>" alt="" />
  33.  
  34. public static MvcHtmlString Image(this HtmlHelper helper, string url, string altText, object htmlAttributes)
  35. {
  36. TagBuilder builder = new TagBuilder("img");
  37. builder.Attributes.Add("src", url);
  38. builder.Attributes.Add("alt", altText);
  39. builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
  40. return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));
  41. }
  42.  
  43. @Html.Image( Url.Content( "~/Content/images/img.png" ),
  44. "alt text",
  45. new { id = "myImage", border = "0" } )
  46.  
  47. <img src="/Content/image/mypic.png" alt="a picture available anywhere" />
Add Comment
Please, Sign In to add comment