RazorLight
RazorLight copied to clipboard
Problem generating list
My code:
var engine = EngineFactory.CreatePhysical(@"D:\engine");
string content = "@foreach (var x in @Model.Items){<li>@x.Name</li>}";
var model = new
{
Name = "John Doe",
Title = "RazorLight",
Items = new List<object>
{
new
{
Name = "a"
},
new
{
Name = "B"
}
}
};
string result = engine.ParseString(content, model);
Error: Additional information: 'object' does not contain a definition for 'Name'
Anonymous models are supported, but in your case you have an anonymous object which has a list of objects that are anonymous objects, which is kinda weird
So possible solutions are:
- Change List < object > to List < string > as you have only 1 property there.
- Or if you are planning to add more properties - create a separate class for that
- Or if you still don't want to create a separate class - try to do that with dynamic, instead of object