0

I want to render razor syntax inside Html.Raw in razor page of mvc. A example is provided bellow. Anyone can tell me is it possible? My goal is rander html text as row html in browser also with some razor syntax like bellow. How is that possible?

@Html.Raw(ViewBag.PageContent); //ViewBag.PageContent contains html+ razor syntax as string text

Above Html.Raw only render the html in browser but razor syntax display as plain text like <title>@ViewData["Title"]</title>

Any idea?

Jona Pie
  • 83
  • 2
  • 8
  • 2
    Seems like a duplicate for [this](https://stackoverflow.com/questions/7523918/asp-net-mvc-execute-razor-from-db-string). – AliK Mar 04 '19 at 15:51
  • No. I have checked your suggested link answer but there is no any working answer. – Jona Pie Mar 04 '19 at 15:56
  • 1
    It is enough to point you in the right direction. Unless you are looking for something else – AliK Mar 04 '19 at 16:00

1 Answers1

0

I think I have a solution for you by using + seperators between the HTML and Razor Syntax.. In this example I have a "RazorPage.cshtml" file that relies on a corresponding "RazorPage.cshtml.cs" file (not shown because it's irrelevant). This example "injects" dynamic data into the title tag and the p tag.

@page
@model ProjectName.Pages.RazorPageModel
@{
    ViewData["Title"] = "RazorPageTitle";
    ViewBag.PageContent = "<title>" + @ViewData["Title"] + "</title>" + "<p>View Source Shows the " + @ViewData["Title"]  + " Tag</p>";
}

<h1>RazorPage</h1>

@Html.Raw(ViewBag.PageContent)