Передача MvcHtmlString с кодом (@if) не работает - PullRequest
0 голосов
/ 15 октября 2019

Я пытаюсь передать 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 внутри панелииспользуя @ Html.Raw, как показано ниже:

<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, чтобы они выполнялись как код, а не отображались как текст, и если да, то как?

...