175

我有两个问题:

  1. 我想知道Html.ActionLink()在 MVC 视图中使用时如何不显示链接文本(实际上,这是Site.Master)。

没有不允许链接文本的重载版本,当我尝试只传入一个空白string时,编译器告诉我它需要一个非空字符串。

我怎样才能解决这个问题?

  1. 我需要将<span>标签放在锚标签中,但它不适用于Html.ActionLink();. 我想看到以下输出:

    跨度文本

如何将标签放在 ASP.NET MVC 中的锚标签内?

4

12 回答 12

340

您可以通过 Url.Action 呈现 url,而不是使用 Html.ActionLink

<a href="<%= Url.Action("Index", "Home") %>"><span>Text</span></a>
<a href="@Url.Action("Index", "Home")"><span>Text</span></a>

并做一个空白的网址,你可以

<a href="<%= Url.Action("Index", "Home") %>"></a>
<a href="@Url.Action("Index", "Home")"></a>
于 2009-12-29T14:49:31.890 回答
16

自定义 HtmlHelper 扩展是另一种选择。 注意: ParameterDictionary 是我自己的类型。您可以替换 RouteValueDictionary 但您必须以不同的方式构造它。

public static string ActionLinkSpan( this HtmlHelper helper, string linkText, string actionName, string controllerName, object htmlAttributes )
{
    TagBuilder spanBuilder = new TagBuilder( "span" );
    spanBuilder.InnerHtml = linkText;

    return BuildNestedAnchor( spanBuilder.ToString(), string.Format( "/{0}/{1}", controllerName, actionName ), htmlAttributes );
}

private static string BuildNestedAnchor( string innerHtml, string url, object htmlAttributes )
{
    TagBuilder anchorBuilder = new TagBuilder( "a" );
    anchorBuilder.Attributes.Add( "href", url );
    anchorBuilder.MergeAttributes( new ParameterDictionary( htmlAttributes ) );
    anchorBuilder.InnerHtml = innerHtml;

    return anchorBuilder.ToString();
}
于 2009-12-29T15:04:14.667 回答
14

如果您需要使用 ajax 或手动创建链接时无法使用的某些功能(使用标签),这是(低和肮脏的)解决方法:

<%= Html.ActionLink("LinkTextToken", "ActionName", "ControllerName").ToHtmlString().Replace("LinkTextToken", "Refresh <span class='large sprite refresh'></span>")%>

您可以使用任何文本代替“LinkTextToken”,它只是被替换,重要的是它不会出现在 actionlink 内的其他任何地方。

于 2012-06-06T11:38:54.230 回答
13

只需使用Url.Action而不是Html.ActionLink

<li id="home_nav"><a href="<%= Url.Action("ActionName") %>"><span>Span text</span></a></li>
于 2009-12-29T14:48:53.310 回答
7

这对我来说一直很有效。它不凌乱而且非常干净。

<a href="@Url.Action("Index", "Home")"><span>Text</span></a>

于 2015-04-07T15:23:26.107 回答
2

我最终得到了一个自定义扩展方法。值得注意的是,当尝试将 HTML 放置在 Anchor 对象中时,链接文本可以位于内部 HTML 的左侧或右侧。出于这个原因,我选择为左右内部 HTML 提供参数——链接文本在中间。左右内部 HTML 都是可选的。

扩展方法 ActionLinkInnerHtml:

    public static MvcHtmlString ActionLinkInnerHtml(this HtmlHelper helper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues = null, IDictionary<string, object> htmlAttributes = null, string leftInnerHtml = null, string rightInnerHtml = null)
    {
        // CONSTRUCT THE URL
        var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
        var url = urlHelper.Action(actionName: actionName, controllerName: controllerName, routeValues: routeValues);

        // CREATE AN ANCHOR TAG BUILDER
        var builder = new TagBuilder("a");
        builder.InnerHtml = string.Format("{0}{1}{2}", leftInnerHtml, linkText, rightInnerHtml);
        builder.MergeAttribute(key: "href", value: url);

        // ADD HTML ATTRIBUTES
        builder.MergeAttributes(htmlAttributes, replaceExisting: true);

        // BUILD THE STRING AND RETURN IT
        var mvcHtmlString = MvcHtmlString.Create(builder.ToString());
        return mvcHtmlString;
    }

使用示例:

这是一个使用示例。对于这个例子,我只想要链接文本右侧的内部 html...

@Html.ActionLinkInnerHtml(
    linkText: "Hello World"
        , actionName: "SomethingOtherThanIndex"
        , controllerName: "SomethingOtherThanHome"
        , rightInnerHtml: "<span class=\"caret\" />"
        )

结果:

这会产生以下 HTML ...

<a href="/SomethingOtherThanHome/SomethingOtherThanIndex">Hello World<span class="caret" /></a>
于 2016-08-10T20:36:51.223 回答
1

我认为这在使用引导程序和一些 glypicons 时可能很有用:

<a class="btn btn-primary" 
    href="<%: Url.Action("Download File", "Download", 
    new { id = msg.Id, distributorId = msg.DistributorId }) %>">
    Download
    <span class="glyphicon glyphicon-paperclip"></span>
</a>

这将显示一个 A 标签,带有指向控制器的链接,上面有一个漂亮的回形针图标来表示下载链接,并且 html 输出保持干净

于 2014-04-18T20:40:20.447 回答
1

这是@tvanfosson 答案的超级扩展。我受到它的启发,并决定让它更通用。

    public static MvcHtmlString NestedActionLink(this HtmlHelper htmlHelper, string linkText, string actionName,
        string controllerName, object routeValues = null, object htmlAttributes = null,
        RouteValueDictionary childElements = null)
    {
        var htmlAttributesDictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);

        if (childElements != null)
        {
            var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);

            var anchorTag = new TagBuilder("a");
            anchorTag.MergeAttribute("href",
                routeValues == null
                    ? urlHelper.Action(actionName, controllerName)
                    : urlHelper.Action(actionName, controllerName, routeValues));
            anchorTag.MergeAttributes(htmlAttributesDictionary);
            TagBuilder childTag = null;

            if (childElements != null)
            {
                foreach (var childElement in childElements)
                {
                    childTag = new TagBuilder(childElement.Key.Split('|')[0]);
                    object elementAttributes;
                    childElements.TryGetValue(childElement.Key, out elementAttributes);

                    var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(elementAttributes);

                    foreach (var attribute in attributes)
                    {
                        switch (attribute.Key)
                        {
                            case "@class":
                                childTag.AddCssClass(attribute.Value.ToString());
                                break;
                            case "InnerText":
                                childTag.SetInnerText(attribute.Value.ToString());
                                break;
                            default:
                                childTag.MergeAttribute(attribute.Key, attribute.Value.ToString());
                                break;
                        }
                    }
                    childTag.ToString(TagRenderMode.SelfClosing);
                    if (childTag != null) anchorTag.InnerHtml += childTag.ToString();
                }                    
            }
            return MvcHtmlString.Create(anchorTag.ToString(TagRenderMode.Normal));
        }
        else
        {
            return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributesDictionary);
        }
    }
于 2014-07-05T19:27:44.287 回答
0

这很简单。

如果你想要一个像字形图标然后是“愿望清单”的东西,

<span class="glyphicon-heart"></span> @Html.ActionLink("Wish List (0)", "Index", "Home")
于 2014-05-21T11:23:58.373 回答
0

请尝试以下可能对您有所帮助的代码。

 @Html.ActionLink(" SignIn", "Login", "Account", routeValues: null, htmlAttributes: new {  id = "loginLink" ,**@class="glyphicon glyphicon-log-in"** }) 
于 2015-03-14T21:42:57.247 回答
0

我使用引导组件的解决方案:

<a class="btn btn-primary" href="@Url.Action("resetpassword", "Account")">
    <span class="glyphicon glyphicon-user"></span> Reset Password
</a>
于 2015-08-26T16:59:48.217 回答
0

请尝试以下代码:

@Html.Raw(@Html.ActionLink("text", "ActionName", "ControllerName", null, new { area = "Home" }).ToHtmlString().Replace("text", "<i class='fa fa-car sub-icon-mg' aria-hidden='true'></i> <span class='mini-sub-pro'>Link Text</span>"))
于 2022-01-13T11:50:53.880 回答