Jaseem Abbas Jaseem Abbas - 3 months ago 33
ASP.NET (C#) Question

Razor tag inside Html.Raw(...)

Is it possible to render Razor in

Html.Raw()
? I have a dynamic page being generated that uses the
Html.Raw()
method to render the page that is created in the controller. My raw html has razor tags in it. That is, I am trying to generate a link by

@Html.Raw(Model.myRawHtmlContainingRazorTags);


where
Model.myRawHtmlContainingRazorTags
contains

<html>
...
...
@Model.TheLink
...
...
<html>


I need the value of
Model.TheLink
to be rendered when
@Html.Raw(Model.myRawHtmlContainingRazorTags)
is called within the cshtml.

Answer

You may have some architectural issues that lead you to have some razor code on your Model property.

Anyway, you can do it by using some external libs such RazorTemplates.

Here is a sample :

var template = Template.Compile(Model.myRawHtmlContainingRazorTags);
@Html.Raw(template.Render(Model));