RazorLight icon indicating copy to clipboard operation
RazorLight copied to clipboard

Problem generating list

Open williamwmd opened this issue 9 years ago • 1 comments

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'

williamwmd avatar Feb 09 '17 17:02 williamwmd

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:

  1. Change List < object > to List < string > as you have only 1 property there.
  2. Or if you are planning to add more properties - create a separate class for that
  3. Or if you still don't want to create a separate class - try to do that with dynamic, instead of object

toddams avatar Feb 09 '17 23:02 toddams