问题描述
这是可行的,因为它以字符串形式返回局部视图渲染的结果:
This works, because it returns the result of partial view rendering in a string:
@Html.Partial("Path/to/my/partial/view")
但我更喜欢使用RenderPartial
,看来我需要写:
But I prefer to use RenderPartial
and it seems I need to write:
@{Html.RenderPartial("Path/to/my/partial/view");}
代替:
@Html.RenderPartial("Path/to/my/partial/view");
让它工作.错误信息:
Compiler Error Message: CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments
是否有更好的方法来代替为一个方法调用打开代码块 @{...}
?
If there any better way instead of opening code block @{...}
just for one method call?
推荐答案
RenderPartial()
是一个写入响应流的void 方法.在 C# 中,void 方法需要;
,因此必须用{ }
括起来.RenderPartial()
is a void method that writes to the response stream. A void method, in C#, needs a;
and hence must be enclosed by{ }
.Partial()
是一种返回 MvcHtmlString.在 Razor 中,您可以调用返回此类字符串的属性或方法,只需一个@
前缀,以将其与页面上的纯 HTML 区分开来.Partial()
is a method that returns an MvcHtmlString. In Razor, You can call a property or a method that returns such a string with just a@
prefix to distinguish it from plain HTML you have on the page.这篇关于带有 Razor 的 Html.RenderPartial() 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!