Reputation: 798
My model for my view contains an object that I need to iterate through and display some HTML for each of its properties (I'm making a way for users to compile razor messages with the @Model syntax). The properties are only displayed if they have the necessary attribute (this part works). However, the recursion does not seem to be occurring as expected.
When debugging and stepping into the recursive call, I see that the fields that should be recursed into are, but the call function is skipped over. That is, following the pointer to show the current executing line of code, it the pointer skips directly from the open to close bracket (with the correct parameters of the recursive call, mind you) without ever executing anything between.
@helper OutputProperties(Type type, string path)
{
<ul>
@foreach (var info in type.GetProperties())
{
var attrib = info.IsDefined(typeof(RazorAccessibleAttribute), false);
if (attrib)
{
<li>
@if (@IsTypePrimitive(info.PropertyType))
{
<a data-value="@(path + "." + info.Name)">@info.Name</a>
}
else
{
<a data-value="@(path + "." + info.Name)" href="#">@info.Name</a>
}
</li>
if (!@IsTypePrimitive(info.PropertyType))
{
OutputProperties(info.PropertyType, path + "." + info.Name);
}
}
}
</ul>
}
It's also worth nothing that I tested a simple recursive function, and this too failed to work.
@helper recurseTest()
{
recurseTest();
}
Again, there is no stackoverflow (heh) error as the recursion never actually happens. I tested my function with both @ and without in front of the call (not sure what the difference is).
Upvotes: 3
Views: 742
Reputation: 1621
Adding "@" in front of the method within should take care of it ...
@OutputProperties(info.PropertyType, path + "." + info.Name);
update : Complete tested code with minor changes to the original
@helper OutputProperties(Type type, string path)
{
<ul>
@foreach (var info in type.GetProperties())
{
var attrib = true;//info.IsDefined(typeof(RazorAccessibleAttribute), false);
if (attrib)
{
<li>
@if (info.PropertyType.IsPrimitive)
{
<a data-value="@(path + "." + info.Name)">@info.Name</a>
}
else
{
<a data-value="@(path + "." + info.Name)" href="#">@info.Name</a>
}
</li>
if ([email protected])
{
@OutputProperties(info.PropertyType, path + "." + info.Name);
}
}
}
</ul>
}
<p>
@OutputProperties(typeof(MvcTestBench.Models.ManagerUser), "Model")
</p>
Upvotes: 5