为什么我无法在App_Code文件夹的razor helper view File中使用Html.RenderPartial? [英] Why I cant use Html.RenderPartial in razor helper view File in App_Code Folder?
问题描述
App_Code文件夹中的简单Razor助手:
Simple Razor Helper in App_Code Folder:
MyHelper.cshtml
MyHelper.cshtml
@using System.Web.Mvc
@helper SimpleHelper(string inputFor){
<span>@inputFor</span>
Html.RenderPartial("Partial");
}
视图/共享文件夹中的简单视图:
Simple View in Views/Shared Folder:
MyView.cshtml
MyView.cshtml
<html>
<head
</head>
<body>
@WFRazorHelper.SimpleHelper("test")
</body>
</html>
视图/共享文件夹中的简单局部视图:
Simple Partial View in Views/Shared Folder:
Partial.cshtml
Partial.cshtml
<h1>Me is Partial</h1>
编译器抛出错误:
CS1061:"System.Web.WebPages.Html.HtmlHelper"基本定义 "RenderPartial",以及《 Kinet Erweiterungsmethode》杂志 'RenderPartial'gefunden werden,die ein erstes Argument vom Typ 'System.Web.WebPages.Html.HtmlHelper'akzeptiert(Fehlt eine Assemblyverweis是否使用了Dirktive oder e?).
CS1061: 'System.Web.WebPages.Html.HtmlHelper' enthält keine Definition für 'RenderPartial', und es konnte keine Erweiterungsmethode 'RenderPartial' gefunden werden, die ein erstes Argument vom Typ 'System.Web.WebPages.Html.HtmlHelper' akzeptiert (Fehlt eine Using-Direktive oder ein Assemblyverweis?).
但是,如果我在MyView.cshtml中调用Html.RenderPartial,一切正常.
But if I call Html.RenderPartial in MyView.cshtml everything works fine.
我想我必须更改一些web.configs,因为MyView中的HtmlHelper来自System.Web.Mvc,而MyHelper.cshtml中的HtmlHelper来自System.Web.WebPages.
I guess I have to change some web.configs, because the HtmlHelper in MyView is taken from System.Web.Mvc and the HtmlHelper in MyHelper.cshtml is taken from System.Web.WebPages.
我该如何解决?
推荐答案
Html
是WebPage的属性,因此您只能在视图内部访问它.您的App_Code文件夹中的自定义帮助程序无权访问.
Html
is a property of the WebPage, so you have access to it only inside the view. The custom helper in your App_Code folder doesn't have access to it.
因此,如果需要在内部使用它,则需要传递HtmlHelper
作为参数:
So you need to pass the HtmlHelper
as parameter if you need to use it inside:
@using System.Web.Mvc.Html
@helper SimpleHelper(System.Web.Mvc.HtmlHelper html, string inputFor)
{
<span>@inputFor</span>
html.RenderPartial("Partial");
}
,然后通过从视图传递HtmlHelper实例来调用自定义帮助器:
and then call the custom helper by passing it the HtmlHelper instance from the view:
<html>
<head>
</head>
<body>
@WFRazorHelper.SimpleHelper(Html, "test")
</body>
</html>
这篇关于为什么我无法在App_Code文件夹的razor helper view File中使用Html.RenderPartial?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!