简体   繁体   English

Razor @helper函数不呈现任何Html

[英]Razor @helper functions not rendering any Html

I have a model with an object property, and a value type id number, and want to create a different editor control depending on the value type number. 我有一个带有object属性的模型和一个值类型id号,并希望根据值类型号创建不同的编辑器控件。

I'm trying to use @help razor contructs, however none of the contents of the helpers are rendered to the page. 我正在尝试使用@help razor contructs,但是没有任何帮助程序的内容呈现给页面。

@helper noEditor()
{
    <div>noEditor</div>
}
@helper stringEditor()
{
    <div>stringEditor</div>
}
@helper intEditor()
{
    <div>intEditor</div>
}
@helper boolEditor()
{
    <div>boolEditor</div>
}
@helper collectionEditor()
{
    <div>collectionEditor</div>
}

@switch(Model.ValueTypeId)
{
    case 1: stringEditor(); break;
    case 2: intEditor(); break;
    case 3: boolEditor(); break;
    case 4: collectionEditor(); break;
    default: noEditor(); break;
}

When I put a break point on the @switch I can see the debugger move to the correct helper, but it skips immediately to the end of the function then exits the switch, nothing is rendered. 当我在@switch上设置@switch我可以看到调试器移动到正确的帮助器,但它会立即跳到函数的末尾,然后退出交换机,不会呈现任何内容。

Any idea on what I'm doing wrong here? 我在这里做错了什么?

To render text with Razor you have to use the @ sign. 要使用Razor渲染文本,您必须使用@符号。 If you change your code to 如果您将代码更改为

@switch(Model.ValueTypeId)
{
    case 1: @stringEditor() break;
    case 2: @intEditor() break;
    case 3: @boolEditor() break;
    case 4: @collectionEditor() break;
    default: @noEditor() break;
}

it should work. 它应该工作。

Alternative you can use Response.Write like this: 您可以选择使用Response.Write

@switch(Model.ValueTypeId)
{
    case 1: Response.Write(stringEditor()); break;
    case 2: Response.Write(intEditor()); break;
    case 3: Response.Write(boolEditor()); break;
    case 4: Response.Write(collectionEditor()); break;
    default: Response.Write(noEditor()); break;
}

which is basically what the @ does in razor. 这基本上是@在剃刀中的作用。

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

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