1

If we write the following code directly in a cshtml file:

<script>
    var xzz = '@Method1("abc", "test5")';
</script>

Then the @Method1 helper functions processes the data and returns a string. The output of the block is as following:

<script>
    var xzz = 'abc test5';
</script>

Which is as expected.

But if the script block comes from the database (in a property of the model) then can't seem to have the helper method (@Method1) or any Razor syntax processed. The output is as following (in the view source). Which is not the expected output:

<script>
    var xzz = '@Method1("abc", "test5")';
</script>

The expected output should be:

<script>
    var xzz = 'abc test5';
</script>

I have tried the followings:

@MvcHtmlString.Create(Html.Raw(Model.property1).ToString())

Html.RenderPartial("_localizer", Model.property1); //The _localizer is a cshtml file

Html.Raw(Model.property1)

@MvcHtmlString.Create(Html.Raw(Model.property1).ToString())

None of them seems to be working.

Rahatur
  • 3,147
  • 3
  • 33
  • 49
  • I think you are adding the extra items as part of the rendering. Do they not need to be present before then? I'm not sure how you can do that. – Peter Smith Oct 24 '18 at 13:38
  • 1
    Just to be sure of what you are asking. You are saying that **@Method1("abc", "test5")** is a string stored in DB and that you want that string to be interpreted as c# code at runtime by the Razor Engine. Is it correct? – Alfredo A. Oct 24 '18 at 14:45
  • Maybe this link might help you https://dzone.com/articles/razor-engine-for-parsing-razor-pages-stored-as-str It handles a problem similar to yours – Alfredo A. Oct 24 '18 at 14:56
  • @AlfredoA. that is correct. The @Method1("abc", "test5") is stored as string in the DB with some other razor code and html, script and style block. I had a look at the dzone sample. That seems to be good for returning content against a virtual view path. Which is not required in this case. – Rahatur Oct 25 '18 at 11:30

0 Answers0