Jona Pie
Jona Pie

Reputation: 83

Render html and razor syntax inside Html.Raw

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?

Upvotes: 0

Views: 2052

Answers (1)

Jonathan A. Reth
Jonathan A. Reth

Reputation: 55

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)

Upvotes: 0

Related Questions