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

后端 未结 11 1727
忘了有多久
忘了有多久 2020-11-28 01:54

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.Mast
相关标签:
11条回答
  • 2020-11-28 02:41

    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")
    
    0 讨论(0)
  • 2020-11-28 02:42

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

        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);
            }
        }
    
    0 讨论(0)
  • 2020-11-28 02:44

    Please try below Code that may help you.

     @Html.ActionLink(" SignIn", "Login", "Account", routeValues: null, htmlAttributes: new {  id = "loginLink" ,**@class="glyphicon glyphicon-log-in"** }) 
    
    0 讨论(0)
  • 2020-11-28 02:45

    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.

    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();
    }
    
    0 讨论(0)
  • 2020-11-28 02:45

    Just use Url.Action instead of Html.ActionLink:

    <li id="home_nav"><a href="<%= Url.Action("ActionName") %>"><span>Span text</span></a></li>
    
    0 讨论(0)
提交回复
热议问题