C# “System.Web.Mvc.HtmlHelper”不包含“RenderPartial”的定义 - ASP.Net MVC

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

'System.Web.Mvc.HtmlHelper' does not contain a definition for 'RenderPartial' - ASP.Net MVC

c#asp.net-mvc

提问by John Davies

I've been trying to run ASP.Net MVC 1.0 on one machine, but can't get past this. I create a new MVC project (C#). It creates all the folders, views, controllers, models etc. All good. Then, when I hit F5, I get the following:

我一直在尝试在一台机器上运行 ASP.Net MVC 1.0,但无法解决这个问题。我创建了一个新的 MVC 项目 (C#)。它创建了所有的文件夹、视图、控制器、模型等。一切都很好。然后,当我按 F5 时,我得到以下信息:

d:\VSCode2008\MVC\MvcApplication1\Views\Shared\Site.Master(19): error CS0117: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'RenderPartial'

d:\VSCode2008\MVC\MvcApplication1\Views\Shared\Site.Master(19):错误 CS0117:“System.Web.Mvc.HtmlHelper”不包含“RenderPartial”的定义

this happens at the following line:

这发生在以下行:

httpHandler.ProcessRequest(HttpContext.Current); in Default.aspx.cs

httpHandler.ProcessRequest(HttpContext.Current); 在Default.aspx.cs

It seems when it is trying to do a RenderPartial() to render the logon partial.

似乎是在尝试执行 RenderPartial() 以呈现登录部分时。

I have version 3.51 of .Net framework installed. I installed version 1.0 of MVC, and the assembly clearly has RenderPartial() as extension methods of HtmlHelper.

我安装了 .Net 框架的 3.51 版。我安装了 MVC 1.0 版,程序集显然有 RenderPartial() 作为 HtmlHelper 的扩展方法。

Anyone seen anything similar? I have found some posts about similar problems with betas and RCs but the suggested fixes have not woredk.

有人见过类似的吗?我发现了一些关于 Beta 版和 RC 版类似问题的帖子,但建议的修复程序并未出现。

I am loving the theory of MVC but it is not letting me play!

我喜欢MVC的理论,但它不让我玩!

回答by dcp

Just trying to rule out the obvious here, but can you make sure have this in the namespaces section of the web.config?

只是想排除这里明显的问题,但是您能确保在 web.config 的命名空间部分中有这个吗?

<add namespace="System.Web.Mvc.Html"/>

回答by Robert Harvey

According to your error message, you are referencing System.Web.Mvc.HtmlHelper. I am looking at the System.Web.Mvcdll in Reflector, and it's telling me that RenderPartialresides in the namespace System.Web.Mvc.Html, not System.Web.Mvc.HtmlHelper.

根据您的错误消息,您正在引用System.Web.Mvc.HtmlHelper. 我正在System.Web.MvcReflector 中查看dll,它告诉我它RenderPartial位于命名空间中System.Web.Mvc.Html,而不是System.Web.Mvc.HtmlHelper.

回答by Nathan

You may think this is dumb, but I just had the same problem. I had a working MVC app, running 1.0.0.0 and all of a sudden it stopped working giving me the same RenderPartial isn't in the definition. Well it turns out that while I was going crazy cleaning up my web.config, I removed this section. When I re-added it, everything worked again. I'm sure this has something to do with how the class extensions load during runtime.

您可能认为这很愚蠢,但我遇到了同样的问题。我有一个正在运行的 MVC 应用程序,运行 1.0.0.0,突然间它停止工作,给我相同的 RenderPartial 不在定义中。事实证明,当我疯狂地清理我的 web.config 时,我删除了这一部分。当我重新添加它时,一切又正常了。我确信这与类扩展在运行时的加载方式有关。

Anyway, re-adding this to my web.config worked on my machine. ;)

无论如何,将它重新添加到我的 web.config 在我的机器上工作。;)

<system.codedom>
    <compilers>
        <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4"
                     type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <providerOption name="CompilerVersion" value="v3.5"/>
            <providerOption name="WarnAsError" value="false"/>
        </compiler>

        <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4"
                     type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <providerOption name="CompilerVersion" value="v3.5"/>
            <providerOption name="OptionInfer" value="true"/>
            <providerOption name="WarnAsError" value="false"/>
        </compiler>
    </compilers>
</system.codedom>

回答by Justin

Since you are referencing it in your Code Behind (ie. Default.aspx.cs) you need to include the namespace at the top of the file.

由于您在代码隐藏(即 Default.aspx.cs)中引用它,因此您需要在文件顶部包含命名空间。

using System.Web.Mvc.Html;

使用 System.Web.Mvc.Html;

is the Namespace that includes the RenderPartial extension method.

是包含 RenderPartial 扩展方法的命名空间。

回答by James White

Another possible problem (depending on your error string) is that the extension method is there, but that the compiler couldn't match up your parameters to the known method parameters. I ran into this using T4MVC (noob) and forgetting to add ActionNames before the name of the action I was calling.

另一个可能的问题(取决于您的错误字符串)是扩展方法存在,但编译器无法将您的参数与已知方法参数匹配。我使用 T4MVC (noob) 遇到了这个问题,但忘记在我调用的操作名称之前添加 ActionNames。