Question

In my ASP.NET MVC 2 web application, I utilize RenderAction to display HTML in what I call the "flash message" area of the web site. By flash area, I mean a highlighted area near the top that contains a message about the previous request, such as Hello <%: Html.DisplayTextFor(m => m.Username) %> when the user logs in.

One flash message that I am working on is a bit more complex. It needs to add a block of Javascript code to the response, but I want to place the script element before the end </body> tag as recommended in the book High Performance JavaScript.

In a normal view, I use <asp:Content ContentPlaceHolderID="FlashScriptElements" runat="server"><script> ... </script></asp:Content> to place script elements at the bottom of the page. Unfortunately, if I attempt the same in a partial view that is rendered with RenderAction, I get the Parser Error "Content controls have to be top-level controls in a content page or a nested master page that references a master page".

Is there something like <asp:Content> that I can use in partial views which will insert the contents into the corresponding spot of the master page?

Was it helpful?

Solution

<asp:Content> is not supported with RenderAction because RenderAction is equivalent to having code that would execute your child action method, gathered the response, and then pasted it in the parent method's view.

What you could do is to add the scripts that you need somewhere in to the HttpContextBase.Items collection. Then in your master page right before the end of the document retrieve the registered scripts and print them out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top