c# - 为什么 Razor 在 html 元素上设置的条件样式总是 html 编码不正确
问题描述
使用 razor 有条件地在 html 元素上设置样式始终将 ' 字符编码并呈现为 ' ; (这会产生不正确的 html 并意味着浏览器不会呈现应用的样式。
我尝试了以下方法:
<tr ID="Question-Answer-@(item.QuestionID)" @(!firstItem ? Html.Raw("style=\'display: none\'").ToString() : "" )>
<tr ID="Question-Answer-@(item.QuestionID)" @(!firstItem ? HttpUtility.HtmlDecode("style=\'display: none\'") : "" )>
<tr ID="Question-Answer-@(item.QuestionID)" @(!firstItem ? "style=\'display: none\'" : "" )>
<tr ID="Question-Answer-@(item.QuestionID)" @(!firstItem ? "style='display: none'" : "")>
<tr ID="Question-Answer-@(item.QuestionID)" @(!firstItem ? new HtmlString("style=\'display: none\'").ToString() : "" )>
两个代码示例都产生以下内容:
<tr ID="Question-Answer-2" style='display: none'>
and I need:
<tr ID="Question-Answer-2" style='display: none'>
解决方案
这是我的问题的答案:
<tr ID="Question-Answer-@(item.QuestionID)" @{if (!firstItem) { <text> style='display: none' </text> } }>
(nb。我仍然不明白为什么我上面尝试的 5 种不同的方法不起作用)
推荐阅读
- json - 为什么映射存储在 React 状态的 JSON 时会出现类型错误?
- javascript - 未设置子组件中输入属性的角度异步管道
- c# - C# 线程任务和多个网页下载的性能问题
- python - 用熊猫中的列名替换列值
- java - 在我替换一个依赖项 jar 中的 .class 文件后,Ant 构建失败并编译失败
- r - 将行追加到 data.table(作为参数传递给函数)
- javascript - 在 Google Closure Compiler 中接收“变量 myFunction 未声明”
- mysql - 用于从表中选择具有最新时间戳的行的 JOOQ 代码
- php - 使用 CloudWatch 在 AWS 上的 Symfony 应用程序中的 PHP 日志目标
- php - 无法将关联字符串转换为关联数组