在链接文本中使用HTML元素创建ActionLink

Gir*_*ffe 34 asp.net-mvc actionlink

在ASP.NET MVC视图中,我想要包含以下形式的链接:

<a href="blah">Link text <span>with further descriptive text</span></a>
Run Code Online (Sandbox Code Playgroud)

试图将<span>元素包含在linkText调用字段中以Html.ActionLink()最终编码(如预期的那样).

有没有推荐的方法来实现这一目标?

Cas*_*per 52

您可以使用Url.Action为您构建链接:

<a href="<% =Url.Action("Action", "Controller")%>">link text <span>with further blablah</span></a>
Run Code Online (Sandbox Code Playgroud)

或使用Html.BuildUrlFromExpression:

<a href="<% =Html.BuildUrlFromExpression<Controller>(c => c.Action()) %>">text <span>text</span></a>
Run Code Online (Sandbox Code Playgroud)


dpl*_*nte 41

如果你喜欢使用剃须刀,这应该工作:

<a href="@Url.Action("Action", "Controller")">link text <span>with further blablah</span></a>
Run Code Online (Sandbox Code Playgroud)