Neovivacity
Neovivacity

Reputation: 119

Passing Razor Markup to view and process it

I've searched the internet for days now with no luck finding this.

My model has a property which holds a chunk of html containing Razor markup. exmaple:

public class ViewModel 
{
    public string Content = "<div>@Html.TextBox(\"UserName\")</div>";
}

In the view, I display that with

@Html.Raw(Server.HtmlDecode(Model.Content).toString())

I need to be able to convert the Razor markup into html, although because the Content is dropped in through the model, the view engine doesn't process it.

Any thoughts?

Upvotes: 0

Views: 256

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

You could use the RazorEngine package which allows you to parse and execute Razor code. This being said I would not recommend you giving your users the power of editing directly Razor templates. You are opening a huge security hole in your website.

There are other templating engines such as DotLiquid for example which are better suited for scenarios where you don't trust user input.

Upvotes: 1

Related Questions