Copied to clipboard

Flag this post as spam?

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


  • Bo Jacobsen 606 posts 2404 karma points
    Jun 02, 2023 @ 11:40
    Bo Jacobsen
    0

    How to use ITemplateRenderer.RenderAsync with IPublishedContentWrapped served through a RenderController

    Hi all.

    Using Umbraco CMS 10.5.1, we are getting html from a Content node, using ITemplateRenderer.RenderAsync to validate the html.

    It fails if we are using a custom controller inherited by RenderController with a custom entity inherited by PublishedContentWrapped.

    It does not initiate the RenderController at all. It's like its looking at the view and decide it cannot bind the models.

    using (var stringWriter = new StringWriter())
    {
        await _templateRenderer.RenderAsync(pageId: 1010, altTemplateId: null, writer: stringWriter);
        return stringWriter.ToString();
    }
    
    public class ListViewModel : PublishedContentWrapped
    {
        public ListViewModel(IPublishedContent content, IPublishedValueFallback publishedValueFallback) : base(content, publishedValueFallback) { }
    
        public List<IPublishedContent>? Results { get; set; }
    
        public string? Query { get; set; }
    
        public IHtmlContent? Pagination { get; set; }
    }
    
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ListViewModel>
    @{
        Layout = "layout.cshtml";
    }
    
    <section>
        <div>
            @await Html.PartialAsync("results", Model)
            @if (Model.Pagination != null)
            {
                @Model.Pagination
            }
        </div>
    </section>
    

    The error we are getting is:

    Umbraco.Cms.Web.Common.ModelBinders.ModelBindingException: 'Cannot bind source content type Umbraco.Cms.Web.Common.PublishedModels.PublicationsArea to model type xxx.xxx.xxx.ListViewModel. 
    The source is a ModelsBuilder type, but the view model is not. The application is in an unstable state and should be restarted.'
    

    Then if we disable the ModelsBuilder, then we get this error:

    Umbraco.Cms.Web.Common.ModelBinders.ModelBindingException: 'Cannot bind source content type Umbraco.Cms.Infrastructure.PublishedCache.PublishedContent to model type xxx.xxx.xxx.ListViewModel.'
    

    What are we missing?

  • Bo Jacobsen 606 posts 2404 karma points
    Jun 06, 2023 @ 10:51
    Bo Jacobsen
    0

    We have also tried to inherit the ListViewModel with ContentModel, and that gives the same error.

    public class ListViewModel : ContentModel
    {
        public ListViewModel(IPublishedContent content) : base(content) { }
    
        public List<IPublishedContent>? Results { get; set; }
    
        public string? Query { get; set; }
    
        public IHtmlContent? Pagination { get; set; }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft