如何调用Html。来自@helper的操作?

时间:2021-04-16 19:39:56

I need to show a list of details about an object, and all child objects of the parent, so I wrote this recursive helper.

我需要显示关于对象和父对象的所有子对象的详细信息列表,因此我编写了这个递归帮助程序。

When I try to run it I get this error:

当我试图运行它时,我得到了这个错误:

Compiler Error Message: CS1928: 'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'Action' and the best extension method overload 'System.Web.Mvc.Html.ChildActionExtensions.Action(System.Web.Mvc.HtmlHelper, string, string, object)' has some invalid arguments

编译错误信息:CS1928: 'System.Web.WebPages.Html。HtmlHelper'不包含'Action'和最佳扩展方法重载'System.Web.Mvc.Html.ChildActionExtensions.Action(System.Web.Mvc)的定义。HtmlHelper, string, string, object)'有一些无效的参数

Source Error:

源错误:

@Html.Action("_PDFUserStories", "Screen", new { screenID = screen.ID})

@Html。动作(“_PDFUserStories”、“Screen”、new {screenID = Screen . id)

Before I was using a partial view and @Html.Action worked fine, but when I try to use a @Html.Action in a helper it does not work. Can I use @Html.Action in a helper? Here is the helper:

在我使用部分视图和@Html之前。动作运行良好,但是当我尝试使用@Html时。帮助者的行为不起作用。我可以用@Html。在辅助行动?这是辅助:

@helper ScreenDetails(UserStoriesApplication.Models.Screen screen)
{
<div style="page-break-after: always;">
    <div class="box col-12">
        <div class="box-header">
            <h2><i class="fa fa-desktop"></i>@screen.Name</h2>
            <div class="box-icon">

            </div>
        </div>
    </div>
    @if (screen.filename != null)
    {
        <img class="img-responsive screenImage" src="~/Content/ScreenImages/@screen.filename" alt="Screen" />
    }
    <div class="story-details" id="story-details">
        @Html.Action("_PDFUserStories", "Screen", new { screenID = screen.ID })
    </div>
</div>

    foreach(var childScreen in screen.Screen1)
    {
       @ScreenDetails(childScreen)

    }

}

1 个解决方案

#1


2  

For a reason unknown to me, the 'Html' that comes with the Helper class is not the same as the 'Html' that comes with views. The Helper one is in the System.Web.WebPages.Html namespace, while the View one is in the System.Web.Mvc namespace. That's why it didn't have the methods you expected. However this can be easily fixed by passing in the Html object to your helper as a parameter. So you could change the signature of your ScreenDetails helper to

不知什么原因,助手类附带的“Html”与视图附带的“Html”并不相同。帮助器在System.Web.WebPages中。Html名称空间,而视图一是在System.Web中。Mvc名称空间。这就是为什么它没有您期望的方法。但是,通过将Html对象作为参数传递给助手,可以很容易地解决这个问题。因此,您可以更改您的屏幕细节助手的签名。

@helper ScreenDetails(UserStoriesApplication.Models.Screen screen,
     System.Web.Mvc.HtmlHelper Html)

then when you call it just pass in the view's Html object

然后,当您调用它时,只需传入视图的Html对象

@MyHelper.ScreenDetails(model, Html)

#1


2  

For a reason unknown to me, the 'Html' that comes with the Helper class is not the same as the 'Html' that comes with views. The Helper one is in the System.Web.WebPages.Html namespace, while the View one is in the System.Web.Mvc namespace. That's why it didn't have the methods you expected. However this can be easily fixed by passing in the Html object to your helper as a parameter. So you could change the signature of your ScreenDetails helper to

不知什么原因,助手类附带的“Html”与视图附带的“Html”并不相同。帮助器在System.Web.WebPages中。Html名称空间,而视图一是在System.Web中。Mvc名称空间。这就是为什么它没有您期望的方法。但是,通过将Html对象作为参数传递给助手,可以很容易地解决这个问题。因此,您可以更改您的屏幕细节助手的签名。

@helper ScreenDetails(UserStoriesApplication.Models.Screen screen,
     System.Web.Mvc.HtmlHelper Html)

then when you call it just pass in the view's Html object

然后,当您调用它时,只需传入视图的Html对象

@MyHelper.ScreenDetails(model, Html)