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

后端 未结 11 1873
忘了有多久
忘了有多久 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: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();
    }
    

提交回复
热议问题