You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET MVC中如何使用Html.Raw编写样式?

ASP.NET MVC 中正确输出样式的方法

你遇到的问题核心是:在代码块@{ }里调用Html.Raw时,只是执行了方法但没有把返回的内容输出到页面上。Html.Raw返回的是MvcHtmlString对象,必须通过@前缀才能将其渲染到HTML中。

以下是两种正确的写法:

  1. 在<style>标签内输出样式内容
<style>
@Html.Raw("table.dataTable tbody td { white-space: nowrap; }")
</style>
  1. 直接输出完整的<style>标签
@Html.Raw("<style> table.dataTable tbody td { white-space: nowrap; } </style>")

补充说明:如果你的样式是固定的静态内容,完全不需要用Html.Raw,直接写普通的<style>标签即可;只有当样式内容是带有HTML特殊字符的动态内容时,才需要用Html.Raw来避免被编码。

内容的提问来源于stack exchange,提问作者Frank Martin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 00:22:02