Rendering An Embeded Macro in a RTE inside a standard MVC View
Hey,
Ive created a standard MVC page which is not part of Umbraco, and im rendering the contents of a rich text editor out, however when the editor emebeds a macro, it doesnt get shown. Id imagine this is beacuse Html.Raw doesnt recognise the Umbraco macro tags and so strips it out. im wondering if theres some sort of umbraco helper hidden away somewhere which will show my content, heres my code, which works fine apart from adding macros into the RTE
Controller:
public class GalleryController : PluginController { public GalleryController() : this(UmbracoContext.Current) { }
public GalleryController(UmbracoContext umbracoContext) : base(umbracoContext) { }
[HttpGet] public ActionResult Lightbox(int id) {
var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); var mediaItem = umbracoHelper.TypedMedia(id); //Get media Item
if(mediaItem != null) { var model = new Lightbox();
and hard coding the home node id, it throws an error:
<!-- Error generating macroContent: 'System.NullReferenceException: Object reference not set to an instance of an object. at umbraco.NodeFactory.Node.getCurrentNodeId() at umbraco.macro.LoadPartialViewMacro(MacroModel macro) at umbraco.macro.renderMacro(Hashtable pageElements, Int32 pageId) at umbraco.presentation.templateControls.Macro.CreateChildControls() at System.Web.UI.Control.EnsureChildControls() at umbraco.presentation.templateControls.Macro.Render(HtmlTextWriter writer) at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) at System.Web.UI.Control.Render(HtmlTextWriter writer) at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) at umbraco.library.RenderMacroContent(String Text, Int32 PageId)' -->
Which appears to be, because the page is not an umbraco node, it cant render the macro.
I can confirm that by using exactly the same code on an actual Umbraco generated page, the RenderMacroContent code works. The issue is therefore not being able to render macro content on an MVC Generated page. However theres also the issue, that you shouldn't need to do RenderMacroContent to display macro content from a rich text editor on a media item, it should just work like any normal Umbraco document, shouldnt it?
Rendering An Embeded Macro in a RTE inside a standard MVC View
Hey,
Ive created a standard MVC page which is not part of Umbraco, and im rendering the contents of a rich text editor out, however when the editor emebeds a macro, it doesnt get shown. Id imagine this is beacuse Html.Raw doesnt recognise the Umbraco macro tags and so strips it out. im wondering if theres some sort of umbraco helper hidden away somewhere which will show my content, heres my code, which works fine apart from adding macros into the RTE
Controller:
public class GalleryController : PluginController
{
public GalleryController() : this(UmbracoContext.Current)
{
}
public GalleryController(UmbracoContext umbracoContext) : base(umbracoContext)
{
}
[HttpGet]
public ActionResult Lightbox(int id)
{
var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
var mediaItem = umbracoHelper.TypedMedia(id); //Get media Item
if(mediaItem != null)
{
var model = new Lightbox();
model.Id = id;
model.Name = mediaItem.Name;
model.RichtextLeft = mediaItem.GetPropertyValue<string>("richtextLeft");
model.RichtextRight = mediaItem.GetPropertyValue<string>("richtextRight");
return View("Lightbox", model);
}
else
{
return View("LightboxImage");
}
}
}
Model:
public class Lightbox
{
public string Name { get; set; }
public int Id { get; set; }
public string RichtextLeft { get; set; }
public string RichtextRight { get; set; }
}
View
@model Lightbox
@{
Layout = null;
}
<div class="left">
@Html.Raw(Model.RichtextLeft)
</div>
<div class="right">
@Html.Raw(Model.RichtextRight)
</div>
I Know this is more an MVC Question than razor, but i couldnt find an MVC specific Board, feel free to move.
Cheers
Any Help is apperciated.
Done some more digging, and it appears the issue is not with the MVC Set up but with Rich Text Areas on a Media Item itself.
Appears to be a bug, that the macro content doesnt get rendered from the media item.
if i try doing:
@umbraco.library.RenderMacroContent(media.GetPropertyValue("richtextLeft").ToString(), 1081)
and hard coding the home node id, it throws an error:
<!-- Error generating macroContent: 'System.NullReferenceException: Object reference not set to an instance of an object. at umbraco.NodeFactory.Node.getCurrentNodeId() at umbraco.macro.LoadPartialViewMacro(MacroModel macro) at umbraco.macro.renderMacro(Hashtable pageElements, Int32 pageId) at umbraco.presentation.templateControls.Macro.CreateChildControls() at System.Web.UI.Control.EnsureChildControls() at umbraco.presentation.templateControls.Macro.Render(HtmlTextWriter writer) at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) at System.Web.UI.Control.Render(HtmlTextWriter writer) at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) at umbraco.library.RenderMacroContent(String Text, Int32 PageId)' -->
Which appears to be, because the page is not an umbraco node, it cant render the macro.
I can confirm that by using exactly the same code on an actual Umbraco generated page, the RenderMacroContent code works. The issue is therefore not being able to render macro content on an MVC Generated page. However theres also the issue, that you shouldn't need to do RenderMacroContent to display macro content from a rich text editor on a media item, it should just work like any normal Umbraco document, shouldnt it?
is working on a reply...