Razor 中的 Html. RenderPartial()语法

这是可行的,因为它返回字符串中部分视图呈现的结果:

@Html.Partial("Path/to/my/partial/view")

但我更喜欢使用 RenderPartial,似乎我需要写:

@{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

如果有任何更好的方法代替打开代码块 @{...}只为一个方法调用?

142863 次浏览
  • 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() 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.

Html.RenderPartial() is a void method - you can check whether a method is a void method by placing your mouse over the call to RenderPartial in your code and you will see the text (extension) void HtmlHelper.RenderPartial...

Void methods require a semicolon at the end of the calling code.

In the Webforms view engine you would have encased your Html.RenderPartial() call within the bee stings <% %>

like so

<% Html.RenderPartial("Path/to/my/partial/view"); %>

when you are using the Razor view engine the equivalent is

@{Html.RenderPartial("Path/to/my/partial/view");}
@Html.Partial("NameOfPartialView")

If you are given this format it takes like a link to another page or another link.partial view majorly used for renduring the html files from one place to another.