Passing a MvcHtmlString with code (@if) not working

43 views Asked by At

I am trying to pass a MvcHtmlString to one of my view that contains if statements which are currently being rendered as text rather than running as code.

The cshtml below is stored in a database table:

    <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>
    }

And then encoded using this 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;
        }

I am then rendering the html inside a panel using @Html.Raw as shown below:

<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>

Do I need to modify the @if statements so that they are run as code rather than displayed as text and if so how?

0

There are 0 answers