Copied to clipboard

Flag this post as spam?

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


  • Tim 67 posts 90 karma points
    Jan 23, 2023 @ 14:33
    Tim
    0

    Setting Modelsbuilder to nothing and using custom controllers & models

    I prefer not to use the Umbraco Modelsbuilder and instead create my own view models and controllers.

    I've followed the route hijacking and custom controller and view model documentation.

    Perhaps I've misunderstood, but I thought doing something like this in my controller would map the CurrentPage properties to my custom model.

    var homepageViewModel = new Homepage(CurrentPage, new PublishedValueFallback(_serviceContext, _variationContextAccessor));
    

    I can see within CurrentPage that my property has a value from the CMS, but it doesn't get mapped to the homepageViewModel, and I don't want to manually map them.

    Here's my simplified example:

    HomepageController.cs

    public class HomepageController : RenderController
    {
        private readonly IVariationContextAccessor _variationContextAccessor;
        private readonly ServiceContext _serviceContext;
    
        public HomepageController(ILogger<HomepageController> logger, ICompositeViewEngine compositeViewEngine, IUmbracoContextAccessor umbracoContextAccessor, IVariationContextAccessor variationContextAccessor, ServiceContext context)
            : base(logger, compositeViewEngine, umbracoContextAccessor)
        {
            _variationContextAccessor = variationContextAccessor;
            _serviceContext = context;
        }
    
        public  IActionResult Homepage()
        {
            // you are in control here!
            var homepageViewModel = new Homepage(CurrentPage, new PublishedValueFallback(_serviceContext, _variationContextAccessor));
            // return a 'model' to the selected template/view for this page.
            return CurrentTemplate(homepageViewModel);
        }
    }
    

    HomepageViewModel.cs

    public class HomepageViewModel : PublishedContentModel
    {
    
        private IPublishedValueFallback _publishedValueFallback;
    
        // ctor
        public HomepageViewModel(IPublishedContent content, IPublishedValueFallback publishedValueFallback)
            : base(content, publishedValueFallback)
        {
            _publishedValueFallback = publishedValueFallback;
        }
    
        public string Headline { get; set; }
    }
    

    Homepage.cshtml

    @using Umbraco.Cms.Core.Models
    
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<HomepageViewModel> 
    @Model.Headline
    

    The view model HomepageViewModel returned to the page has a null value for Heading, but I can see it in the CurrentPage / Properties / Heading / Non-public Members / _sourceValue which contains the published value from the CMS.

    If I have to use a custom mapper so be it, but I can't see the value in creating a model and putting in the CurrentPage as a parameter if its not going to map it.

    Any help would be much appreciated :)

  • Tim 67 posts 90 karma points
    Jan 23, 2023 @ 15:34
    Tim
    0

    -- update --

    I've managed to achieve what I wanted by changing the view model to this:

    public class HomepageViewModel : PublishedContentModel
    {
    
        private readonly IPublishedContent _content;
        private IPublishedValueFallback _publishedValueFallback;
    
        // ctor
        public HomepageViewModel(IPublishedContent content, IPublishedValueFallback publishedValueFallback)
            : base(content, publishedValueFallback)
        {
            _publishedValueFallback = publishedValueFallback;
            _content = content;
        }
    
    
        public string Heading => _content.Value<string>("heading");
    }
    

    But I'd rather my view model looked like this..

            public string Heading { get; set; }
    
  • Luuk Peters 85 posts 331 karma points
    Jan 26, 2023 @ 16:09
    Luuk Peters
    0

    I'm curious why you don't want to use the models builder? It saves so much time in many cases. In many cases you don't even need to write a controller, except when you need additional fields. In your example you seem to do everything manually to get to the same result as the Models Builder.

Please Sign in or register to post replies

Write your reply to:

Draft