繁体   English   中英

ASP.NET MVC 3:多个 Html.RenderAction() 问题

[英]ASP.NET MVC 3: Multiple Html.RenderAction() Problem

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

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

无论如何,我注意到如果我使用以下语法@{ RenderSection("SectionName", true); } @{ 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.)

〜/模型/实体.edmx

(Generated with Entity Framework Wizard.)


* 编辑 *

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

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

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

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

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

希望这会有所帮助。

对于 RenderAction,您必须将它与所示的 bello 一样的大括号

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

希望这对某人有所帮助。

尝试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM