Michael Tot Korsgaard
Michael Tot Korsgaard

Reputation: 4014

rendering html from cs file to cshtml

I'm creating some html code from a cs file, which i want to be rendered one the actual site. But what I get rendered is the "raw" html code, not the rendered. What am I doing wrong?

the .cshtml code:

<div>
    @class1.createhtml();
</div>

the .cs code:

public static string createhtml()
{
    return "<p>hi</p>";
}

Upvotes: 3

Views: 11169

Answers (2)

Tim S.
Tim S.

Reputation: 56536

<div>
    @Html.Raw(class1.createhtml())
</div>

You should be very careful that you don't put unescaped user-input into this string, or you open up the door to injection attacks - treat this with the caution that you might treat appending strings to make a SQL query: avoid it if at all possible, or be very careful.

Upvotes: 15

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38468

You should use Html.Raw method in your view:

@Html.Raw(class1.createhtml())

Upvotes: 6

Related Questions