0

我正在尝试将 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>
    }

然后使用这个 C# 编码:

public MvcHtmlString GetKeyIndicatorDefinitionById(int id)
        {
            ViewItDataSourceEntities ndtms2Data = new ViewItDataSourceEntities();

            var keyIndicatorDefinition = (from rows in ndtms2Data.KeyIndicators
                where rows.Id == id
                select rows.Description).FirstOrDefault();

            var encodedKeyIndicatorDefinition = MvcHtmlString.Create(keyIndicatorDefinition);

            return encodedKeyIndicatorDefinition;
        }

然后,我使用@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>

我是否需要修改@if 语句,以便它们作为代码运行而不是显示为文本,如果需要,如何?

4

0 回答 0