简体   繁体   English

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

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

I am faced with a problem with ASP.NET MVC 3 with the Razor rendering engine (C#).我在使用 Razor 渲染引擎(C#)时遇到了 ASP.NET MVC 3 的问题。 I am using multiple Html.RenderAction() methods within a view and only the layout is rendered.我在一个视图中使用多个Html.RenderAction()方法,并且只呈现布局。

One important note before continuing: I can't copy any of the code because I do not have the intellectual property rights to do so.继续之前的一个重要说明:我不能复制任何代码,因为我没有这样做的知识产权。 :( :(

Anyway, I noticed that if I used the following syntax @{ RenderSection("SectionName", true); }无论如何,我注意到如果我使用以下语法@{ RenderSection("SectionName", true); } @{ RenderSection("SectionName", true); } instead of @RenderSection("SectionName", true) there was a generic exception that simply says that it was unable to render the sections with a stack trace that seemed to say that there might be some asynchronous problem. @{ RenderSection("SectionName", true); }而不是@RenderSection("SectionName", true)有一个通用异常,它只是说它无法使用似乎说可能存在一些异步问题的堆栈跟踪来呈现这些部分。 Between, I am using synchronous controllers so maybe this is a lead, but I don't know much on synchronous/asynchronous controllers and when one should use them for certain situations.在这两者之间,我使用的是同步控制器,所以也许这是一个线索,但我对同步/异步控制器以及何时应该在某些情况下使用它们知之甚少。

To put everything in context, here is what I am trying to do in code/pseudo code/site structure...为了将所有内容放在上下文中,这就是我在代码/伪代码/站点结构中尝试做的事情......

~/View/Shared/_ViewStart.cshtml ~/View/Shared/_ViewStart.cshtml

(Selects a layout according to certain conditions.)

~/View/Shared/_Layout1.cshtml ~/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 ~/View/Shared/_Layout2.cshtml

(another layout)

~/View/Controller1/Action1.cshtml ~/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 ~/View/Controller1/Action2.cshtml

(similar to Action1.cshtml)

~/Utilities/ModelManager.cs ~/Utilities/ModelManager.cs

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

~/Controller/Controller1.cs ~/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 ~/Controller/Controller2.cs

(another controller similar to Controller1)

~/Controller/Table.cs ~/Controller/Table.cs

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

~/Controller/Content.cs ~/Controller/Content.cs

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

~/Model/Entities.edmx 〜/模型/实体.edmx

(Generated with Entity Framework Wizard.)


* Edit * * 编辑 *

The @Html.Action(...) worked, but I would really like to know why the @Html.RenderAction(...) didn't work. @Html.Action(...)有效,但我真的很想知道为什么@Html.RenderAction(...)无效。 Any suggestions are welcome.欢迎任何建议。 :) :)

As an alternative, I would suggest trying to switch to:作为替代方案,我建议尝试切换到:

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

This extension helper works similarly to RenderAction, but returns MvcHtmlString, instead of writing directly to the Output buffer (Response stream).此扩展助手的工作方式与 RenderAction 类似,但返回 MvcHtmlString,而不是直接写入 Output 缓冲区(响应流)。

The Html.RenderAction is a method returns void. Html.RenderAction 是一个返回 void 的方法。 So you must put a ";"所以你必须放一个“;” at the end.在最后。 As for the exception, you might want to try to debug into it and see what the variables are set to etc if you would like to stick with that helper method.至于异常,如果您想坚持使用该辅助方法,您可能想尝试对其进行调试并查看变量设置等。

Hopefully that helps.希望这会有所帮助。

For RenderAction, you have to put it withing a brace like the one shown bello对于 RenderAction,您必须将它与所示的 bello 一样的大括号

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

Hope this help someone.希望这对某人有所帮助。

Try this:尝试这个:

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

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

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