问题
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