asp.net-mvc Html.RenderPartial 给了我奇怪的重载错误?

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

Html.RenderPartial giving me strange overload error?

asp.net-mvcasp.net-mvc-3razorrenderpartial

提问by naspinski

I made a test partial page named _Test.cshtmland put it in the same directory as my view that will be calling it, here it is:

我制作了一个名为_Test.cshtml的测试部分页面,并将它放在与我将调用它的视图相同的目录中,这里是:

<div>hi</div>

And in the calling cshtml view, I simply put:

在调用 cshtml 视图中,我简单地输入:

@Html.RenderPartial("_Test")

Which gives me the error:

这给了我错误:

CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments

CS1502:“System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)”的最佳重载方法匹配有一些无效参数

I have also tried the full path with the same result.

我也尝试了具有相同结果的完整路径。

I am very confused as to why this is acting this way, I assume I am missing something simple?

我很困惑为什么会这样,我想我错过了一些简单的东西?

回答by Luká? Novotny

You are getting this error because Html.RenderXXXhelpers return void - they have nothing to return because they are writing stuff directly* to response. You should use them like this:

您收到此错误是因为Html.RenderXXX助手返回 void - 他们没有任何东西可返回,因为他们直接写东西* 来响应。你应该像这样使用它们:

@{ Html.RenderPartial("_Test"); }

There is also Html.Partial helper, which will work with your syntax, but I'd not recommend using it unless you have to, because of performance (it first composes given partial view into string, and then parent view puts it into response*).

还有 Html.Partial 助手,它可以与您的语法一起使用,但我不建议使用它,除非您必须,因为性能(它首先将给定的部分视图组合成字符串,然后父视图将其放入响应* )。

* this is not entirely true, they are actually being rendered into ViewContext.Writer and once whole page is rendered and composed, the whole thing goes to response

* 这并不完全正确,它们实际上被渲染到 ViewContext.Writer 中,一旦整个页面被渲染和组合,整个事情就会进入响应