【问题标题】:ASP.NET MVC 3: Multiple Html.RenderAction() ProblemASP.NET MVC 3:多个 Html.RenderAction() 问题
【发布时间】:2011-09-29 06:22:00
【问题描述】:

我遇到了带有 Razor 渲染引擎 (C#) 的 ASP.NET MVC 3 的问题。我在一个视图中使用了多个Html.RenderAction() 方法,并且只呈现了布局。

继续之前的重要说明:我无法复制任何代码,因为我没有这样做的知识产权。 :(

无论如何,我注意到,如果我使用以下语法 @{ RenderSection("SectionName", true); } 而不是 @RenderSection("SectionName", true),则会出现一个通用异常,它只是说它无法使用似乎说可能存在的堆栈跟踪来呈现这些部分一些异步问题。在这两者之间,我使用的是同步控制器,所以也许这是一个线索,但我对同步/异步控制器以及何时应该在某些情况下使用它们知之甚少。

为了将所有内容放在上下文中,这是我在代码/伪代码/站点结构中尝试做的事情......

~/View/Shared/_ViewStart.cshtml

(Selects a layout according to certain conditions.)

~/View/Shared/_Layout1.cshtml

<! doctype HTML>
<html>
    <head>
        <!-- some irrelevant content here... -->
    </head>

    <body>
        <div id="page">
            <div id="header">
                @RenderSection("Header", true)
            </div>

            <div id="content">
                <div id="main1">
                    @RenderSection("Table1", true)
                    @RenderSection("Table2", true)
                </div>

                <div id="main2">
                    @RenderSection("Content", true)
                </div>
            </div>

            <div id ="footer">
                @RenderSection("Footer", true)
            </div>
        </div>
    </body>
</html>

~/View/Shared/_Layout2.cshtml

(another layout)

~/View/Controller1/Action1.cshtml

@section Header
{
    @RenderPage("~/Views/Shared/Sections/Header")
}

@section Footer
{
    @RenderPage("~/Views/Shared/Sections/Footer")
}

@section Table1
{
    @{ RenderAction("Table1", "Table");  }
}

@section Table2
{
    @{ RenderAction("Table2", "Table");  }
}

@section Content
{
    @{ RenderAction("Action", "Content"); }
}

~/View/Controller1/Action2.cshtml

(similar to Action1.cshtml)

~/Utilities/ModelManager.cs

public abstract class ModelManager : Controller
{
    //Some useful code for controllers here...
}

~/Controller/Controller1.cs

public class Controller1 : ModelManager
{
    #region Get Methods

    public ViewResult Action1()
    {
        return View();
    }

    public ViewResult Action2()
    {
        return View();
    }

    #endregion  

    #region Post Methods

    public ViewResult Action1(FormCollection form)
    {
        return View();
    }

    public ViewResult Action2(FormCollection form)
    {
        return View();
    }

    #endregion
}

~/Controller/Controller2.cs

(another controller similar to Controller1)

~/Controller/Table.cs

(Only important thing to note is that the actions are returning PartialViewResult.)

~/Controller/Content.cs

(Only important thing to note is that the action is returning PartialViewResult.)

~/Model/Entities.edmx

(Generated with Entity Framework Wizard.)


* 编辑 *

@Html.Action(...) 有效,但我真的很想知道为什么 @Html.RenderAction(...) 无效。欢迎任何建议。 :)

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-3 web-applications razor


    【解决方案1】:

    作为替代方案,我建议尝试切换到:

    @Html.Action("actionMethod","controller")
    

    此扩展助手的工作方式与 RenderAction 类似,但返回 MvcHtmlString,而不是直接写入输出缓冲区(响应流)。

    Html.RenderAction 是一个返回 void 的方法。所以你必须放一个“;”在末尾。至于异常,如果您想坚持使用该辅助方法,您可能想尝试对其进行调试并查看变量设置等。

    希望对您有所帮助。

    【讨论】:

    • 感谢您的帮助,我明天将尝试 Html.Action(...) 并进一步调试,看看我是否遗漏了什么。
    • 抱歉花了这么长时间......奇怪的是 Html.Action() 方法有效。就像您说的那样,这是一种替代方案,但是两者之间是否存在显着的性能差异。另外,您知道为什么 Html.RenderAction() 不起作用(它的末尾有分号)吗?
    • 我猜没有例外。您也许可以尝试通过在调用 RenderAction 之前抛出异常来查看发生了什么,然后调试传递它(跳过)并查看带有异常的确切堆栈跟踪以及 RenderAction 行上的所有内容。确保将构建设置为调试。我使用这个技巧很难找到查看错误。
    【解决方案2】:

    试试这个:

    @section Table1
    {
        RenderAction("Table1", "Table");
    }
    

    【讨论】:

      【解决方案3】:

      对于 RenderAction,你必须像上面展示的那样用一个大括号来放置它

      @{Html.RenderAction(...);}
      

      希望这对某人有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-26
        • 2010-10-18
        • 2011-07-12
        • 2011-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多