Search code examples
.netasp.net-mvcasp.net-mvc-4razorengine

How to Parse Razor string using Model in MVC


I have below razor string in my project as given below.

string razorString = "This mail is from @Model.email";
var model = new { email = "mann@gmail.com" };

I need to parse razorString with given model. I have seen Razor.Parse method in RazorEngine but that method is obsolete now and replaced with Engine.Razor. So how can i achieve same with new method. Please suggest.

After using Engine.Razor.RunCompile(template, "templateKey", null, model) i got below error

enter image description here


Solution

  • string template = "This mail is from @Model.email";
    var model = new { email = "mann@gmail.com" };
    
    string result = Engine.Razor.RunCompile(template, "templateKey", null, model);