Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Michelle 12 posts 122 karma points
    Sep 08, 2019 @ 19:41
    Michelle
    0

    How do you use a macro to display a partial view?

    I would like to display a partial view in a rich text editor. The code in my partial view is:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>
    @{ var content = Model.OfType<content-block>(); }
    
    <article class="content">
        <div class="row">
            <div class="block-text">
                @content.Text
            </div>
        </div>
    </article>
    

    I have created a macro but get the following error:

    Umbraco.Web.Mvc.ModelBindingException: Cannot bind source type Umbraco.Web.Models.PartialViewMacroModel to model type Umbraco.Core.Models.IPublishedContent.

    Is it possible to render a view in a Rich Text Editor with a macro? I feel this is probably simple! Thank you in advance.

  • Michelle 12 posts 122 karma points
    Sep 08, 2019 @ 20:58
    Michelle
    100

    I found the answer after a few hours of trying everything! Firstly, it is important to note the reason for the error is because the partial view inherits one model and the macro has it's own so it was always going to clash.

    To get around this I used the macro query builder. To use this you will need to create a marco partial and then on the top right the option will be available.

    I then copied out the html from my partial and it worked successfully. Use GetPropertyValue() to render the actual content and not the default @item.Name as this just gives you the name of the item (handy if you are creating a list) but I wanted to render content within a RTE. To obtain the ID, select the info tab on the item you want to display. Here is the finished code:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        var selection = Umbraco.TypedContent(1442).Children()
                        .Where(x => x.IsVisible())
                        .Where(x => x.Id == 4647);
     }
    
    @foreach(var item in selection){
        <article class="content">
            <div class="row">
                <div class="block-text">
                    @item.GetPropertyValue("Text")
                </div>
            </div>
        </article>
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft