Html.RenderPartial 给了我奇怪的重载错误? [英] Html.RenderPartial giving me strange overload error?

查看:20
本文介绍了Html.RenderPartial 给了我奇怪的重载错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

hi

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

@Html.RenderPartial("_Test")

这给了我错误:

<块引用>

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

我也试过完整路径,结果相同.

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

解决方案

您收到此错误是因为 Html.RenderXXX 助手返回 void - 他们没有什么可返回,因为他们直接将内容* 写入回复.你应该像这样使用它们:

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

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

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

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

<div>hi</div>

And in the calling cshtml view, I simply put:

@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

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?

解决方案

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

@{ 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*).

* 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

这篇关于Html.RenderPartial 给了我奇怪的重载错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆