1

I am trying to solve this issue from last 6 hours, but couldn't complete it.

here I have one template script in my Razor view.

<script type="text/template" id="Blog_Post_Template">   

    {{if DescType == "FULL"}}
            ${FDesc}
    {{/if}}

    {{if DescType == "SORT"}}
            ${SDesc}
    {{/if}}

</script>

Now, what I want is to use Razor function inside this template.

Let's take @Html.Raw(), I want to use @Html.Raw() in ${FDesc}, and ${SDesc}.

I tried many escape sequences like,

@Html.Raw("'${FDesc}'")
@Html.Raw("\'${FDesc}\'")

And mane more solutions, but I came up at the point where I started.

Input is like, A simple text with html parameters

<p> Test this </p>

Output will be a html

Appreciate, if anyone can resolve it..

7
  • What are you trying to achieve ? If you want to execute C# code, put it in code block/ or use @ prefix. Commented Jul 27, 2017 at 14:26
  • text/template provides less support, I want to use @html.raw() function inside script..but it does not allow me.. Commented Jul 27, 2017 at 14:28
  • What have you tried ? What error /output are you getting ? What is your expected output ? Commented Jul 27, 2017 at 14:29
  • and what is DescType ? Is it a C# variable ? js variable ? Commented Jul 27, 2017 at 14:31
  • all are js variables.. Commented Jul 27, 2017 at 14:33

4 Answers 4

1

It doesn't seem that you understand the effect of the client-server nature of HTTP here. Razor codes runs server-side, while your JavaScript/template code will run client-side, after the server has already done its work and returned a response. As a result, something like @Html.Raw("'${FDesc}'") will literally output '${FDesc}', not the raw HTML that ${FDesc} will eventually evaluate to. If you need to ensure that that template variable is printed without being HTML-escaped, then you need to rely on functionality in your JavaScript templating library for that, not Razor.

Sign up to request clarification or add additional context in comments.

Comments

1

Finally, After 3 days of R&D, I found solution.

BTW, we can't use razor is template as @chris said, but there is alternative And it's very very simpler.

Answer is Html tag inside template, see below example.

{{html Value}}

and my full code now looks like,

<script type="text/template" id="Blog_Post_Template">   

    {{if DescType == "FULL"}}
          {{html FDesc}} // which will now give me pure html
    {{/if}}

    {{if DescType == "SORT"}}
          {{html SDesc}}
    {{/if}}

</script>

Comments

1

Wrap your template in some html element such as div

<script type="text/template" id="Blog_Post_Template">   
<div>
    {{if DescType == "FULL"}}
            ${FDesc}
    {{/if}}

    {{if DescType == "SORT"}}
            ${SDesc}
    {{/if}}
</div>
</script>

Comments

0

Not entirely sure what's server and client side but it maybe I'm not familiar with the templating you are using, but server side evaluation of a variable and then rendering client side with no elements avaiable can be achieved using the <text></text> elements:

<script type="text/template" id="Blog_Post_Template">   
    @if(DescType == "FULL")
    {
        <text>${FDesc}</text>
    }
    ...
</script>

Maybe this will help?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.