ASP.NET MVC Razor中三元表达式字符串渲染带引号问题排查
Fixing Unwanted Quotes in Razor Rendered Text
The issue here is that you’ve wrapped your Razor expression in literal double quotes inside the <h3> tag. Those quotes are being rendered as part of the text content, which is why you see them displayed on the page.
Immediate Fix
Remove the surrounding double quotes around your Razor expression. Here’s the corrected code:
<h3 class="panel-title">@(System.Configuration.ConfigurationManager.AppSettings["OST"] == "true" ? "Out-of-State Travel" : "Out-Service Training/Out-of-State Travel")</h3>
Cleaner, More Maintainable Approach (Optional)
For better readability and reusability, you can extract the logic into a Razor code block first:
@{ // Retrieve the setting and determine the appropriate title var isOSTEnabled = System.Configuration.ConfigurationManager.AppSettings["OST"] == "true"; var panelTitle = isOSTEnabled ? "Out-of-State Travel" : "Out-Service Training/Out-of-State Travel"; }
Then reference the variable directly in your HTML:
<h3 class="panel-title">@panelTitle</h3>
This makes your code easier to update later, especially if you need to reuse this title logic in multiple places or add additional conditions.
内容的提问来源于stack exchange,提问作者Bryan Dellinger
相关产品推荐
相关产品推荐

