asp.net-mvc 带有 Razor 的 Html.RenderPartial() 语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6980823/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:23:20  来源:igfitidea点击:

Html.RenderPartial() syntax with Razor

asp.net-mvcasp.net-mvc-3razor

提问by artvolk

This works, because it returns the result of partial view rendering in a string:

这是有效的,因为它以字符串形式返回局部视图渲染的结果:

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

But I prefer to use RenderPartialand it seems I need to write:

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

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

instead of:

代替:

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

To get it to work. Error message:

让它工作。错误信息:

 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?

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

回答by Ofer Zelig

  • RenderPartial()is a void methodthat 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.

  • RenderPartial()是一个写入响应流的void 方法。C# 中的 void 方法需要;,因此必须用 括起来{ }

  • Partial()是一种返回MvcHtmlString的方法。在 Razor 中,您可以调用返回此类字符串的属性或方法,只需一个@前缀,以将其与页面上的纯 HTML 区分开来。

回答by Nicholas Murray

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...

Html.RenderPartial() 是一个 void 方法 - 您可以通过将鼠标放在代码中对 RenderPartial 的调用上来检查方法是否为 void 方法,您将看到文本(扩展名)void HtmlHelper.RenderPartial...

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

void 方法需要在调用代码的末尾使用分号。

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

在 Webforms 视图引擎中,您可以将 Html.RenderPartial() 调用封装在蜂刺 <% %> 中

like so

像这样

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

when you are using the Razor view engine the equivalent is

当您使用 Razor 视图引擎时,等效项是

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

回答by RouR

@Html.Partial("NameOfPartialView")

回答by Sankar

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.

如果你得到这种格式,它就像一个链接到另一个页面或另一个链接。部分视图主要用于将 html 文件从一个地方复制到另一个地方。