Copied to clipboard

Flag this post as spam?

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


  • ItsShane 2 posts 83 karma points
    Mar 22, 2022 @ 18:57
    ItsShane
    0

    Umbraco v9 - question about GetModelPropertyType method on a generated model (document type) in v9

    I'm following along with the "Surface Controller" videos on youtube from the official YT channel. I'm on the "Surface Controllers: Creating content" video.

    So far the code looks like:

        [HttpPost]
        [ValidateUmbracoFormRouteString]
        public IActionResult Submit(ArticleModel model)
        {
            if (!ModelState.IsValid)
            {
                return CurrentUmbracoPage();
            }
            var contentService = Services.ContentService;
            var parentId = new Guid("4505b81c-0dad-4b6b-bf69-daad24ba4524");
    
            var article = contentService.Create(model.Title, parentId, Article.ModelTypeAlias);
    
            contentService.SaveAndPublish(article);
    
            return RedirectToCurrentUmbracoPage();
        }
    

    Everything works fine so far. However the next part of the video you're supposed to get the IPublishedPropertyType of the Summary property from the Article class from Umbraco.Cms.Web.Common.PublishedModels.

    That code is supposed to look like:

    var summaryProperty = Article.GetModelPropertyType(d => d.Summary);
    

    However, when I try to do this, the GetModelPropertyType expects an IPublishedSnapshotAccessor type to be it's first parameter.

    enter image description here

    What do I do? Is there another method to call somewhere? I know the video is using v8 so is there a way to do this in v9? Anyway's thanks to anyone who read this and I hope this helps someone else in the future.

  • Ambert van Unen 175 posts 819 karma points c-trib
    Mar 22, 2022 @ 21:08
    Ambert van Unen
    101

    You actually need to do exactly as stated in the message. The method now requires an instance of IPublishedSnapShotAccessor

    Use dependency injection to add the IPublishedSnapShotAccessor to your class

    So you get something like this:

    public class TheClassYouWorkIn
    {
        private readonly IPublishedSnapshotAccessor _snapshotAccessor;
    }
    //The constructor
    public TheClassYouWorkIn(IPublishedSnapshotAccessor snapshotAccessor){
        _snapshotAccessor = snapshotAccessor;
    }
    

    Now you have the _snapshotAccessor available for usage. The GetModelPropertyType method requires it, so you can add it:

    var summaryProperty = Article.GetModelPropertyType(_snapshotAccessor, d => d.Summary);
    

    Done ! :-)

  • ItsShane 2 posts 83 karma points
    Mar 22, 2022 @ 21:29
    ItsShane
    1

    Thank you sooooo soo much Ambert!

Please Sign in or register to post replies

Write your reply to:

Draft