Closed SpringHgui closed 2 years ago
Hi, you need to make custom razor engine template class and inhert RazorEngineTemplateBase then you can put any custom members in this new template class
like this https://github.com/adoconnection/RazorEngineCore/wiki/Strongly-typed-model
public class CustomTemplateBase : RazorEngineTemplateBase
{
public dynamic ViewBag { get; set; }
}
return template.Run(instance =>
{
instance.ViewBag = new
{
title = "hello"
},
instance.Model = model;
});
// cshtml
<h1>@ViewBag.title</h1>
'object' does not contain a definition for 'title'
Hi, everything is fine, but although ViewBag is dynamic it does not give you permission to access anonymous object proeprties.
RazorEngine engineCore = new RazorEngine();
var compiledTemplate = engineCore.Compile<CustomTemplateBase>("<h1>@ViewBag.title</h1>");
Console.WriteLine(compiledTemplate.Run(instance =>
{
instance.ViewBag = new AnonymousTypeWrapper(new
{
title = "hello"
});
}));
worked
How to use ViewBag