1

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.TheLinkto be rendered when @Html.Raw(Model.myRawHtmlContainingRazorTags) is called within the cshtml.

1 Answer 1

3

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));
2
  • I have an API which generates raw HTML list. Clicking on an item in the list, redirects the page with certain arguments. There arguments are fetched from the current HTML page. The arguments are present as hidden fields in the current HTML. The API is called and the raw HTML is assigned to a model. This model is then passed to the view. Now the items in the list have a razor content again, which caused this issue to arise. Am I doing something wrong here? Mar 10, 2015 at 18:11
  • what seems unusual for me is that your API generates raw HTML which contains Razor Tag. If you can change it to deliver view name instead of raw html, it will be easier to render with tools like RenderPartial.
    – Perfect28
    Mar 10, 2015 at 18:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.