C# 将 HTML 放在 Html.ActionLink() 中,加上无链接文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1974980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:20:18  来源:igfitidea点击:

Putting HTML inside Html.ActionLink(), plus No Link Text?

c#.netasp.net-mvcactionlink

提问by MegaMatt

I have two questions:

我有两个问题:

  1. I'm wondering how I can display no link text when using Html.ActionLink()in an MVC view (actually, this is Site.Master).
  1. 我想知道Html.ActionLink()在 MVC 视图中使用时如何不显示链接文本(实际上,这是Site.Master)。

There is not an overloaded version that does not allow link text, and when I try passing in just a blank string, the compiler tells me it needs a non-empty string.

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

How can I fix this?

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

  1. I need to put <span>tags within the anchor tag, but it's not working with Html.ActionLink();. I'd like to see the following output:

    Span text

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

    跨度文本

How can I put tags inside of the anchor tag in ASP.NET MVC?

如何在 ASP.NET MVC 中的锚标记内放置标记?

采纳答案by David

Instead of using Html.ActionLink you can render a url via Url.Action

您可以通过 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>

And to do a blank url you could have

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

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

回答by Craig Stuntz

Just use Url.Actioninstead of Html.ActionLink:

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

<li id="home_nav"><a href="<%= Url.Action("ActionName") %>"><span>Span text</span></a></li>

回答by tvanfosson

A custom HtmlHelper extension is another option. Note: ParameterDictionary is my own type. You could substitute a RouteValueDictionary but you'd have to construct it differently.

自定义 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();
}

回答by Goran Obradovic

Here is (low and dirty) workaround in case you need to use ajax or some feature which you cannot use when making link manually (using tag):

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

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

You can use any text instead of 'LinkTextToken', it is there only to be replaced, it is only important that it does not occur anywhere else inside actionlink.

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

回答by Terry Kernan

I thought this might be useful when using bootstrap and some glypicons:

我认为这在使用引导程序和一些 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>

This will show an A tag, with a link to a controller, with a nice paperclip icon on it to represent a download link, and the html output is kept clean

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

回答by DanKodi

It's very simple.

这很简单。

If you want to have something like a glyphicon icon and then "Wish List",

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

<span class="glyphicon-heart"></span> @Html.ActionLink("Wish List (0)", "Index", "Home")

回答by william-kid

Here is an uber expansion of @tvanfosson's answer. I was inspired by it and decide to make it more generic.

这是@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);
        }
    }

回答by Ahsanul

Please try below Code that may help you.

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

 @Html.ActionLink(" SignIn", "Login", "Account", routeValues: null, htmlAttributes: new {  id = "loginLink" ,**@class="glyphicon glyphicon-log-in"** }) 

回答by dbarth

This has always worked well for me. It's not messy and very clean.

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

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

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

回答by Carlos Toledo

My solution using bootstrap components:

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

<a class="btn btn-primary" href="@Url.Action("resetpassword", "Account")">
    <span class="glyphicon glyphicon-user"></span> Reset Password
</a>