gpt4 book ai didi

c# - 如何在 <%= %> 中打印文本?

转载 作者:太空宇宙 更新时间:2023-11-03 14:01:56 25 4
gpt4 key购买 nike

是否有更简单的方法来执行以下操作?

<% if (Model.XY == 10) { %>
10
<%} else { %>
<%= Html.ActionLink("10", "SetXY", new { [...] }) %>
<% } %>

我在想类似的事情

<%= Model.XY == 10 ? "10" : Html.ActionLink() %>

最佳答案

ActionLink 助手返回 MvcHtmlString。值“10”是一个字符串。 MvcHtmlString 和 String/string 之间没有隐式转换。

要解决这个问题,您应该使用以下方法从字符串中创建一个 MvcHtmlString:

 new MvcHtmlString("10");

从那里您可以将代码简化为:

 <%= Model.XY == 10 ? new MvcHtmlString("10") : Html.ActionLink("10", "SetXY", new { [...] }) %>

关于c# - 如何在 <%= %> 中打印文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10535645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com