简体   繁体   中英

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#). I am using multiple Html.RenderAction() methods within a view and only the layout is rendered.

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); } 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. 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

(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.)


* Edit *

The @Html.Action(...) worked, but I would really like to know why the @Html.RenderAction(...) didn't work. 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).

The Html.RenderAction is a method returns 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

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

Hope this help someone.

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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