Razor语法防止在ActionLink中转义HTML

12

我有一个ASP MVC 3网站,我们想在操作链接中添加一些样式。

我希望HTML的代码是这样的<a href="/somepath/someaction"><span class="someclass">some text</span> some more text</a>,但我不知道如何告诉Razor正确地呈现它的<span>部分。

我到目前为止尝试过的:

@Html.ActionLink("<span class="someclass">some text</span> some more text", SomeAction, SomeController);

结果是一个链接,看起来像这样:<span class="someclass">一些文本</span>更多的文本

@Html.ActionLink("<text><span class="someclass">some text</span></text> some more text", SomeAction, SomeController);

结果是一个看起来像这样的链接:<text><span class="someclass">一些文本</span></text>更多的文本

@Html.ActionLink(<text>"<span class="someclass">some text</span> some more text"</text>, SomeAction, SomeController);

导致编译错误。

你有什么想法吗?

1个回答

24

ActionLink 方法无法使用 HTML 标签。

你需要创建一个普通的 <a> 标签,然后使用 @Url.Action(...) 替代 href 属性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接