问题
I'm using RazorEngine in a templating solution. I have to use anonymous objects for my model data. The code:
string template = @"
Name: @Model.name
@foreach(var person in @Model.People) {
<row>
<column header=""Name"" width=""5cm"">@person.Name</column>
<column header=""Age"" width=""5cm"">@person.Age</column>
</row>
}";
var personList = new List<object>();
personList.Add(new { Name = "Bob", Age = 25 });
personList.Add(new { Name = "Peter", Age = 30 });
var model = new { name = "Henry", People = personList };
string result = Razor.Parse(template, model);
I'm getting an RuntimeBinderException ('object' does not contain a definition for 'Name').
Can anyone help me?
Thank you!
回答1:
Anonymous types having internal properties is a poor .NET framework design decision, in my opinion.
Here is a quick and nice extension to fix this problem i.e. by converting the anonymous object into an ExpandoObject right away.
Please refer to this question's answer: https://stackoverflow.com/a/5670899/544283.
来源:https://stackoverflow.com/questions/16737760/razorengine-using-anonymous-objects-runtimebinderexception