Razor and HTML Helpers

前端 未结 4 1105
隐瞒了意图╮
隐瞒了意图╮ 2020-12-29 12:33

I\'m trying to port the old HTML.Image helper, that I\'m sure everyone has used at one point or another, and I\'m having issues. The following compiles fine:



        
相关标签:
4条回答
  • 2020-12-29 13:06

    I would try wrapping the image url in a call to Url's Content method as so

    @Url.Content("~/Content/my-img.png")
    

    that should convert the relative url to absolute when the page is flushed to the browser

    0 讨论(0)
  • 2020-12-29 13:10

    I had the same problem and i had used MvcHtmlString as return type for these 2 extension method and it works

    public static class ImageHelper
    {
        public static MvcHtmlString Image(this HtmlHelper helper, string id, string url, string alternateText)
        {
            return Image(helper, id, url, alternateText, null);
        }
    
        public static MvcHtmlString Image(this HtmlHelper helper, string id, string url, string alternateText,
                                   object htmlAttributes)
        {
            var builder = new TagBuilder("img");
    
            builder.GenerateId(id);
    
            builder.MergeAttribute("alt", alternateText);
            builder.MergeAttribute("src",url);
    
            builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
    
            return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
        }
    }
    
    0 讨论(0)
  • 2020-12-29 13:17
        public static HtmlString Image(this HtmlHelper helper, string id, string url, string alternateText)
        {
            return Image(helper, id, url, alternateText, null);
        }
    
        public static HtmlString Image(this HtmlHelper helper, string id, string url, string alternateText, object htmlAttributes)
        {
            // Instantiate a UrlHelper 
            var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
    
            // Create tag builder
            var builder = new TagBuilder("img");
    
            // Create valid id
            builder.GenerateId(id);
    
            // Add attributes
            builder.MergeAttribute("src", urlHelper.Content(url));
            builder.MergeAttribute("alt", alternateText);
            builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
    
            // Render tag
            var ret = new MvcHtmlString(builder.ToString(TagRenderMode.SelfClosing));
    
            return ret;
        }
    

    just like this tested and works perfect. i needed something like this to concatenate the image name form the model thanks.

    and this one works too.

     <img src="@Url.Content("~/Content/Images/Flags/" + c.CountryCode + ".jpg") " alt=""/>
    
    0 讨论(0)
  • 2020-12-29 13:31

    The Razor view engine will automatically HTML-escape strings rendered by @-blocks.
    To render actual HTML, you need to write an IHtmlString implementation in the @-block.

    Change your method to return HtmlString instead of string.

    0 讨论(0)
提交回复
热议问题