简体   繁体   English

MVC Razor @helper函数无法正确递归?

[英]MVC Razor @helper function unable to recurse properly?

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). 我的视图模型包含一个我需要迭代的对象,并为每个属性显示一些HTML(我正在为用户创建一种使用@Model语法编译razor消息的方法)。 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. 同样,没有stackoverflow(heh)错误,因为递归从未实际发生过。 I tested my function with both @ and without in front of the call (not sure what the difference is). 我用@和没有在电话前测试我的功能(不确定区别是什么)。

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 (!@info.PropertyType.IsPrimitive)
                    {
                        @OutputProperties(info.PropertyType, path + "." + info.Name);
                    }
            }
        }
    </ul>
}

<p>
    @OutputProperties(typeof(MvcTestBench.Models.ManagerUser), "Model")
</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM