using html.ActionLink but not html encoding

痴心易碎 提交于 2019-12-25 16:47:28

问题


I wish to return the following output

<a href="#"><img src="/images/icons/tick.png" alt="" />More info</a>

If i do the following the content is html encoded.

<%= Html.ActionLink("<img src='/images/icons/tick.png' />More info", "OrderRegion", "Campaign", new {id = Model.Campaign.Id}, null) %>

How can i disable the html encoding?


回答1:


you can create an HtmlHelper to this

public static class HtmlHelpers
{
    public static string MyActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
    {
        var tagActionLink = htmlHelper.ActionLink("[replace]", actionName, controllerName, routeValues, htmlAttributes).ToHtmlString();
        return tagActionLink.Replace("[replace]", linkText);
    }
}

in .aspx:

<%= Html.MyActionLink("<img src='/images/icons/tick.png' />More info", "OrderRegion", "Campaign", new {id = 15}, null) %>



回答2:


I think you are better off using Url.Action here, e.g.:

<a href="<%= Url.Action("OrderRegion", "Campaign", new {id = Model.Campaign.Id}) %>">
    <img src="/images/icons/tick.png" alt="" />More info
</a> 



回答3:


If you are building an MvcHtmlString and want to include a helper-styled absolute path, you can use

VirtualPathUtility.ToAbsolute("~/")

and then append your link text, controller, and actions with fancy HTML/entites as string literals in the MvcHtmlString construction.



来源:https://stackoverflow.com/questions/3969803/using-html-actionlink-but-not-html-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!