7

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

3 Answers 3

13

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the help, I'll have a try at the Html.Action(...) tomorrow and debug further on to see if I missed something.
Sorry for having taken so long... Strangely the Html.Action() method worked. Like you said it is an alternative, but is there a significant performance difference between the two. Also, do you have any idea why the Html.RenderAction() doesn't work (it has the semicolon at the end)?
Without the exception I would be guessing. You could perhaps try to see what's happening by throwing an exception just before you call RenderAction, then debug pass it (step over) and see the exact stack trace with exception and all on the RenderAction line. Be sure to se the build to debug. I use this trick with hard to find View bugs.
0

Try this:

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

Comments

0

For RenderAction, you have to put it withing a brace like the one shown bello

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

Hope this help someone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.