In Umbraco 7.0.3 I:
- Created a Data Type called Macro Container with Property editor of Macro container
- Created Document Type called Contact Form with Property called Body with Type Macro Container
- Created Partial View called _contactForm.cshtml (in Views\MacroPartials)
- Created Macro called Contact Form with MVC Partial view _contactFrom.cshtml
- Added Content of type Contact Form called Contact Us
- Added Contact Form macro to the Macro Container property called Body in my Contact Us page
I then have a Surface Controller
that I call with some AJAX
to display the page (more specifically the Body property of the page):
public class JsController : SurfaceController
{
public ActionResult GetPage(int id)
{
var page = new Node(id);
if (page == null || page.GetProperty("body") == null)
return Content(@"Hmm, something went wrong. Unable to find what you're looking for.");
return Content(page.GetProperty("body").Value);
}
}
This setup almost works but the problem is that instead of the rendered form, what is returned is:
<!--?UMBRACO_MACRO macroAlias="ContactForm" /-->
So now I need to render this macro\form\partial view...I think that I probably need to do it in the Controller, but if I can do it on the other side (via Javascript) that would work as well. Is there an Umbraco function I can call in the controller to render a macro based on the page id and macro alias?
So after spending several hours fuming at how painfully stupid the
Umbraco
team made this process, reading threads like this and this, I finally figured out a fairly ugly, but working way...things would have been so much more simple if thePublishedContentRequest
class constructor was notinternal
!Anyways, here's what I had to do: 1) Extend
EnsurePublishedContentRequestAttribute
2) Redirect to an action decorated with this attribute that redirects back to my GetPage action and retrieve the
PCR
from theSession
. Now we can render our macro:This works, but this is some pretty hacky stuff. If the
Umbraco
team made thePublishedContentRequest
constructorpublic
, this could have been much, much cleaner. Of course, there's probably a better way to do this, if so, I'm all ears.