asp.net-mvc 在 MVC5 中使用 Razor 渲染局部视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24439526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rendering partial views with Razor in MVC5
提问by Mourndark
I'm trying to get a partial view to render using Razor in MVC5. When I use
我正在尝试在 MVC5 中使用 Razor 获得局部视图以进行渲染。当我使用
@{ Html.RenderPartial("ViewName", model); }
I get the parser error:
我收到解析器错误:
Unexpected "{" after "@" character. Once inside the body of a code block (@if {}, @{}, etc.) you do not need to use "@{" to switch to code.
“@”字符后出现意外的“{”。一旦进入代码块的主体(@if {}、@{} 等),您就不需要使用“@{”来切换到代码。
When I remove the {}, i.e.:
当我删除{},即:
@Html.RenderPartial("ViewName", model);
I get the compilation error
我收到编译错误
Cannot implicitly convert type 'void' to 'object'.
无法将类型“void”隐式转换为“object”。
What am I doing wrong?
我究竟做错了什么?
回答by Chris Pratt
You haven't posted the context of that code, but that error only really happens when you're using @directly within a code block without any HTML wrappings. For example:
您还没有发布该代码的上下文,但只有当您@直接在没有任何 HTML 包装的代码块中使用时,才会真正发生该错误。例如:
@if (true) {
@{ Html.RenderPartial(...); }
}
Would give you the error, while:
会给你错误,而:
@if (true) {
<div>
@{ Html.RenderPartial(...); }
</div>
}
Would be fine. You could also solve it by just removing the code block for Html.RenderPartialentirely, including the @:
会好的。您也可以通过完全删除代码块来解决它Html.RenderPartial,包括@:
@if (true) {
Html.RenderPartial(...);
}
回答by Developer
You may also use @Html.Partial("~/View/Home/myview.cshtml")
你也可以使用 @Html.Partial("~/View/Home/myview.cshtml")
It returns string while Html.RenderPartialcalls Write internally, and returns void.
它在Html.RenderPartial内部调用 Write 时返回字符串,并返回 void。
回答by Believe2014
This is wrong:
这是错误的:
@Html.RenderPartial("ViewName", model);
This is correct:
这是对的:
@{ Html.RenderPartial("ViewName", model); }
The parsing error might be caused by the partial view's content. For example, if you have an email address, make sure you use @@ to properly escape the @ sign.
解析错误可能是由部分视图的内容引起的。例如,如果您有电子邮件地址,请确保使用 @@ 正确转义 @ 符号。
Unexpected "{" after "@" character. Once inside the body of a code block (@if {}, @{}, etc.) you do not need to use "@{" to switch to code.
“@”字符后出现意外的“{”。一旦进入代码块的主体(@if {}、@{} 等),您就不需要使用“@{”来切换到代码。
回答by Anthony Shaw
can you show the code surrounding your RenderPartial? I'm guessing you're in a loop, if block, or some other type of code block. If so, you would just call
你能显示围绕你的 RenderPartial 的代码吗?我猜你在循环、if 块或其他类型的代码块中。如果是这样,您只需致电
Html.RenderPartial("ViewName", model);
回答by Alex Leyva
Please review your code, you might be getting that error because you are using this code:
@{ Html.RenderPartial("ViewName", model); }inside any other @{}clause. Please, read again carefully the error message you get in the browser, you don't need to use the @{}to switch to code because you already are inside, so, just remove @{and }and you should getting it working properly.
请检查您的代码,您可能会收到该错误,因为您正在使用此代码:
@{ Html.RenderPartial("ViewName", model); }在任何其他@{}子句中。请再次仔细阅读您在浏览器中得到错误信息,你不需要使用@{}到切换到代码,因为你已经在里面,所以,只是删除@{和},你应该得到它正常工作。
回答by Deepak Singla
Use @Html.Partial(). It has three overloads, pass the parameters as per your requirement.
使用@Html.Partial(). 它有三个重载,根据您的要求传递参数。

