MiP
MiP

Reputation: 6442

Output plain html between @ tag in Asp.Net MVC

If I have an if statement in a cshtml file:

<div>
    @if (true)
    {
        @Html.Raw("(True)")
    }
</div>

How can I print "(True)" to the html? Currently I need to use Html.Raw which is unsafe, is there other safer way?

EDIT: removed html tags.

Upvotes: 1

Views: 617

Answers (2)

Travis Acton
Travis Acton

Reputation: 4440

I don't agree with your security assessment of html.raw but you could use this as an alternative:

    @if(true)
            {
                <text> (True) </text>
            }

Upvotes: 0

Karthik Elumalai
Karthik Elumalai

Reputation: 1612

Razor’s @: and syntax can then be used for scenarios where you want to avoid using an HTML element within a code container block, and need to more explicitly denote a content region.

Official source:https://weblogs.asp.net/scottgu/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax

Hope it will be helpful , kindly let me know your thoughts or feedbacks

Thanks Karthik

Upvotes: 1

Related Questions