Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting masterName on MVC view causes error when calling RenderAction
    primarykey
    data
    text
    <p>I have a Masterpage (site.master) that calls a View using RenderAction. At the moment the View returns "hello world". </p> <p>site.master:</p> <pre><code>&lt;%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %&gt; &lt;html&gt; &lt;head id="Head1" runat="server"&gt; &lt;title&gt;&lt;asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server" /&gt;&lt;/title&gt; &lt;asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server" /&gt; &lt;/head&gt; &lt;body&gt; &lt;% Html.RenderAction("Test", "Charts"); %&gt; &lt;asp:ContentPlaceHolder ID="ContentPlaceHolder3" runat="server" &gt; &lt;p&gt;site.master&lt;/p&gt; &lt;/asp:ContentPlaceHolder&gt; &lt;asp:ContentPlaceHolder ID="ContentPlaceHolder4" runat="server" /&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Test.aspx:</p> <pre><code>&lt;%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage&lt;dynamic&gt;" %&gt; hello world! </code></pre> <p>ChartsController.cs:</p> <pre><code>public ActionResult Test() { return View(); } </code></pre> <p>If I update the View to pass in the name of the Masterpage explicitly I get an error when I call RenderAction.</p> <p>ChartsController.cs:</p> <pre><code>public ActionResult Test() { return View(null, "site"); } </code></pre> <p>Error:</p> <p>Content controls have to be top-level controls in a content page or a nested master page that references a master page.</p> <pre><code>Stack Trace: [HttpException (0x80004005): Content controls have to be top-level controls in a content page or a nested master page that references a master page.] System.Web.UI.MasterPage.CreateMaster(TemplateControl owner, HttpContext context, VirtualPath masterPageFile, IDictionary contentTemplateCollection) +8857854 System.Web.UI.Page.get_Master() +54 System.Web.UI.Page.ApplyMasterPage() +15 System.Web.UI.Page.PerformPreInit() +45 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +328 </code></pre> <p>How do I go about setting the master page that I want the view to use? Ultimately I will be setting the Masterpage dynamically using a custom ViewEngine (by overriding VirtualPathProviderViewEngine.FindView).</p> <pre><code>if ( String.IsNullOrEmpty(masterName) ){ masterName = "site"; } </code></pre> <p>When I set the masterName property in my ViewEngine and then call RenderAction from site.master I get the same error as when I set the masterName property in the Action.</p> <p>I am using:<br> Visual Studio 2010<br> MVC 3<br> IIS Express </p> <p>edited: added full site.master markup</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload