Skip to content

Commit 95fb282

Browse files
committed
support for dictionaries in anonymous model #91
1 parent d1fdbdc commit 95fb282

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

RazorEngineCore.Tests/TestCompileAndRun.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,68 @@ public async Task TestCompileAndRun_DynamicModel_ListsAsync()
357357
Assert.AreEqual(expected, actual);
358358
}
359359

360+
361+
[TestMethod]
362+
public void TestCompileAndRun_DynamicModel_Dictionary1()
363+
{
364+
RazorEngine razorEngine = new RazorEngine();
365+
366+
var model = new
367+
{
368+
Dictionary = new Dictionary<string, object>()
369+
{
370+
{ "K1", "V1"},
371+
{ "K2", "V2"},
372+
}
373+
};
374+
375+
var template = razorEngine.Compile(@"
376+
@foreach (var key in Model.Dictionary.Keys)
377+
{
378+
<div>@key</div>
379+
}
380+
<div>@Model.Dictionary[""K1""]</div>
381+
<div>@Model.Dictionary[""K2""]</div>
382+
");
383+
384+
string actual = template.Run(model);
385+
string expected = @"
386+
<div>K1</div>
387+
<div>K2</div>
388+
<div>V1</div>
389+
<div>V2</div>
390+
";
391+
Assert.AreEqual(expected, actual);
392+
}
393+
394+
395+
[TestMethod]
396+
public void TestCompileAndRun_DynamicModel_Dictionary2()
397+
{
398+
RazorEngine razorEngine = new RazorEngine();
399+
400+
var model = new
401+
{
402+
Dictionary = new Dictionary<string, object>()
403+
{
404+
{ "K1", new { x = 1 } },
405+
{ "K2", new { x = 2 } },
406+
}
407+
};
408+
409+
var template = razorEngine.Compile(@"
410+
<div>@Model.Dictionary[""K1""].x</div>
411+
<div>@Model.Dictionary[""K2""].x</div>
412+
");
413+
414+
string actual = template.Run(model);
415+
string expected = @"
416+
<div>1</div>
417+
<div>2</div>
418+
";
419+
Assert.AreEqual(expected, actual);
420+
}
421+
360422
[TestMethod]
361423
public void TestCompileAndRun_TestFunction()
362424
{

RazorEngineCore/AnonymousTypeWrapper.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,26 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
4141

4242
bool isEnumerable = typeof(IEnumerable).IsAssignableFrom(type);
4343

44-
if (isEnumerable && !(result is string))
44+
if (result is IDictionary dictionary)
4545
{
46-
result = ((IEnumerable<object>) result)
46+
List<object> keys = new List<object>();
47+
48+
foreach(object key in dictionary.Keys)
49+
{
50+
keys.Add(key);
51+
}
52+
53+
foreach(object key in keys)
54+
{
55+
if (dictionary[key].IsAnonymous())
56+
{
57+
dictionary[key] = new AnonymousTypeWrapper(dictionary[key]);
58+
}
59+
}
60+
}
61+
else if (isEnumerable && !(result is string))
62+
{
63+
result = ((IEnumerable<object>)result)
4764
.Select(e =>
4865
{
4966
if (e.IsAnonymous())
@@ -55,7 +72,7 @@ public override bool TryGetMember(GetMemberBinder binder, out object result)
5572
})
5673
.ToList();
5774
}
58-
75+
5976

6077
return true;
6178
}

0 commit comments

Comments
 (0)