Using HTML tags inside linkText of Html.ActionLink

前端 未结 4 934
星月不相逢
星月不相逢 2020-12-06 16:15

Is it possible to use HTML tags in the linkText of Html.ActionLink? For instance, if I wanted to bold part of the text of a link I would try something similar to this:

相关标签:
4条回答
  • 2020-12-06 16:58

    This works for me:

    @Html.Raw(@Html.ActionLink("XXX", "Index", new { }, new { @class = "FormBtn" }).ToHtmlString().Replace("XXX","<u>Back to List</u>"))
    

    Essentially use the ActionLink to create the html with a placeholder for what you want to replace ('XXX'), then convert it an HTML String, replace the placeholder with your markup, render the string as HTML.Raw.

    0 讨论(0)
  • 2020-12-06 16:59

    It's not possible, but you could create a HtmlHelper for this, see here or use Url.Action instead what I would recommend you.

    0 讨论(0)
  • 2020-12-06 17:09

    The Html.ActionLink helper HTML encodes the link text which prevents you from embedding HTML in the link text.

    For this same reason you cannot use Html.ActionLink and pass in an tag to make an image a hyperlink.

    For basic styling of a link, I'd recommend using one of the Html.ActionLink overloads to specify a CSS style via the anonymous object syntax like so...

    @Html.ActionLink("Please Edit Me", "Edit", null, new { style="font-weight:bold;" })
    

    Unfortunately, that applies bold to the entire text of the hyperlink when what you're wanting is just the word Edit to be bold. In which case I would do this...

    <a href="@Url.Action("Edit")">Please <b>Edit</b> Me</a>
    

    ... or this ...

    <a href="@Url.Action("Edit")">Please <span style="font-weight:bold;">Edit</span> Me</a>
    
    0 讨论(0)
  • 2020-12-06 17:12

    No; it's not possible.
    You need to manually write an <a> tag.

    0 讨论(0)
提交回复
热议问题