在Actionlink中做原始HTML

时间:2015-02-23 19:07:17

标签: razor

我有这个动作链接

@Html.ActionLink("<i class='fa fa-sign-out'></i>" + ecHelpers.GetLabel("Log out"), "Logout", "MyAccountSurface", new { Class = "" })

如何将html变为html而不是像&lt; i class =&#39; fa fa-sign-out&#39;&gt;&lt; / i&gt; ?

3 个答案:

答案 0 :(得分:0)

对于这种情况,您可以使用@Url.Action

<a href="@Url.Action("Logout", "MyAccountSurface")">
    <i class='fa fa-sign-out'></i>@ecHelpers.GetLabel("Log out")
</a>

注意:我不确定您ecHelpers.GetLabel的来电在语法上是否正确,但您明白了。

答案 1 :(得分:0)

通常,您将帮助调用嵌套在您希望显示的标记内,如:

<i class='fa fa-sign-out'>
    @Html.ActionLink(ecHelpers.GetLabel("Log out"), "Logout", "MyAccountSurface", new { Class = "" })
</i>

答案 2 :(得分:0)

我使用了这种扩展方法,以使其更容易:

    public static MvcHtmlString RawActionLink(this HtmlHelper htmlHelper, string linkHtml, string actionName, string controllerName, object routeValues, object htmlAttributes)
    {
        var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
        var url = urlHelper.Action(actionName, controllerName, routeValues);
        var attributeDictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
        var concatenatedHtmlAttributes = string.Join(" ", attributeDictionary.Select(a => string.Format("{0}=\"{1}\"", a.Key, HttpUtility.HtmlAttributeEncode(a.Value.ToString()))));            
        var link = string.Format("<a href=\"{0}\" {1}>{2}</a>", url, concatenatedHtmlAttributes, linkHtml);
        return new MvcHtmlString(link);
    }
相关问题