C# 正在传递代码(@if)不起作用的MvcHtmlString

C# 正在传递代码(@if)不起作用的MvcHtmlString,c#,if-statement,razor,view,mvchtmlstring,C#,If Statement,Razor,View,Mvchtmlstring,我试图将一个MvcHtmlString传递给我的一个视图,该视图包含当前呈现为文本而不是作为代码运行的if语句 下面的cshtml存储在数据库表中: <h3>SUPPORTING INFORMATION</h3> @if (Model.UnmetNeedSubstanceId == "1") { <h3>Opiate and/or crack users (OCU)</h3> <

我试图将一个MvcHtmlString传递给我的一个视图,该视图包含当前呈现为文本而不是作为代码运行的if语句

下面的cshtml存储在数据库表中:

    <h3>SUPPORTING INFORMATION</h3>
    @if (Model.UnmetNeedSubstanceId == "1") {
            <h3>Opiate and/or crack users (OCU)</h3>
            <p>Set out below are the estimated number of opiate and / or crack users (OCUs) in your local authority area and rate of unmet need. Collectively, they have a significant impact on crime, unemployment, safeguarding children and long-term benefit reliance. 
              These prevalence estimates give an indication of the number of OCUs in your local area that are in need of specialist treatment and the rate of unmet need gives the proportion of those not currently in treatment. This data can be used to inform commissioning 
              and any subsequent plans to address unmet treatment need. Specific rates for addressing unmet need will be determined locally.
            </p>
    }
    @if (Model.UnmetNeedSubstanceId == "4") {
            <h3>Alcohol</h3>
            <p>Set out below are the estimated number of dependent drinkers in your local authority area and rate of unmet need. The prevalence estimate gives an indication of the number of adults in your local area that are in need of specialist alcohol treatment and the 
              rate of unmet need gives the proportion of those not currently in treatment. This data can be used to inform commissioning and any subsequent plans to address unmet treatment need. Specific rates for addressing unmet need will be determined locally. 
              Effective structured treatment for alcohol dependent adults will be an essential element of a local integrated alcohol harm reduction strategy. Ambition for addressing unmet need for treatment will be based on local need in the context of that strategy.
            </p>
    }
        <h3>TECHNICAL DEFINITION</h3>
    @if (Model.UnmetNeedSubstanceId == "1") {
            <h3>Opiate and/or crack users (OCU)</h3>
            <p>
              Estimated number of opiate and / or crack users (OCUs) aged 15-64 in your local authority area and rate of unmet need. Please see 
              <a target="_blank" href="https://phi.ljmu.ac.uk/wp-content/uploads/2018/07/Estimates-of-the-Prevalence-of-Opiate-Use-and-or-Crack-Cocaine-Use-2014-15-Sweep-11-report.pdf">Estimates of the Prevalence of Opiate Use and/or Crack Cocaine Use, 2014/15: Sweep 11 report</a>
              for details on how the prevalence rates were calculated. Rates of unmet need were calculated using figures from NDTMS for numbers in treatment aged 15-64.
            </p>
    }
    @if (Model.UnmetNeedSubstanceId == "4") {
            <h3>Alcohol</h3>
            <p>
              Estimated number of people aged 18 or over dependent on alcohol in England and the rate of unmet need. Please see 
              <a target="_blank" href=https://www.sheffield.ac.uk/polopoly_fs/1.693546!/file/Estimates_of_Alcohol_Dependence_in_England_based_on_APMS_2014.pdf>https://www.sheffield.ac.uk/polopoly_fs/1.693546!/file/Estimates_of_Alcohol_Dependence_in_England_based_on_APMS_2014.pdf</a> 
              for details on how the prevalence rates were calculated. Rates of unmet need were calculated using figures from NDTMS for numbers in treatment aged 18 and over within each stated year (alcohol only and non-opiate and alcohol substance groups).
            </p>
    }
然后,我使用@html.Raw在面板中呈现html,如下所示:

<div id="definition-panel" class="panel panel-default col-md-offset-3">
    <div class="panel-heading">
        <a class="collapseLegend">
            <p id="definition-title"><i class="more-less-5 glyphicon glyphicon-plus"></i> Supporting information and definition</p>
        </a>
    </div>
    <div class="panel-body collapsible5">
        @Html.Raw(Model.KeyIndicatorDefinition)
    </div>
</div>

支持信息和定义

@Html.Raw(Model.KeyIndicatorDefinition)

我是否需要修改@if语句,使其以代码形式运行而不是以文本形式显示?如果需要,如何修改?

请检查此解决方案,它肯定会起作用。MvcHtmlString不会像您期望的那样处理razor代码块。将显示层逻辑作为字符串存储在数据存储中不是一个好的设计策略。维护这一点将是一场噩梦,特别是在很长一段时间过去之后,您忘记了您所做的事情的细节,更不用说如果代码由其他人维护会发生什么。使用不同的局部视图会更好,在主视图中,根据数据加载适当的局部视图。
<div id="definition-panel" class="panel panel-default col-md-offset-3">
    <div class="panel-heading">
        <a class="collapseLegend">
            <p id="definition-title"><i class="more-less-5 glyphicon glyphicon-plus"></i> Supporting information and definition</p>
        </a>
    </div>
    <div class="panel-body collapsible5">
        @Html.Raw(Model.KeyIndicatorDefinition)
    </div>
</div>