7

I was thinking about giving end users the ability to drop Partial Views (controls) into the information being stored in the database. Is there a way to execute a string I get from the database as part of the Razor view?

2
  • Can you elaborate on what you are trying to do? Are you trying to store string "@Html.RenderPartial(\"SomeView.cshtml\")" in db, then get Razor engine to execute that in your template? Commented Sep 23, 2011 at 3:31
  • Daryl, that is exactly what I am talking about. Commented Sep 23, 2011 at 15:57

1 Answer 1

8

Update (I forgot all about this)

I had asked this question previously (which lead me to create RazorEngine) Pulling a View from a database rather than a file

I know of at least two: RazorEngine, MvcMailer

I have a bias towards RazorEngine as it's one that I've worked on but I have a much simpler one at Github called RazorSharp (though it only supports c#)

These are all pretty easy to use.

RazorEngine:

string result = RazorEngine.Razor.Parse(razorTemplate, new { Name = "World" });

MvcMailer

I haven't used this one so I can't help.

RazorSharp

RazorSharp also supports master pages.

string result = RazorSharp.Razor.Parse(new { Name = "World" }, 
                                       razorTemplate, 
                                       masterTemplate); //master template not required

Neither RazorSharp, nor RazorEngine support any of the Mvc helpers such as Html and Url. Since these libraries are supposed to exist outside of Mvc and thus require more work to get them to work with those helpers. I can't say anything about MvcMailer but I suspect the situation is the same.

Hope these help.

Sign up to request clarification or add additional context in comments.

3 Comments

This is not able to parse if the content has got some special characters in javascript code? like var x = '(http|ftp|https):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?';
@Rahatur just like in cshtml you'll probably have to escape with two @@ but it's been so long I'm just guessing here.
That might be the issue! Will give that a try.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.