Copied to clipboard

Flag this post as spam?

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


  • Carl Sjöholm 44 posts 327 karma points
    Oct 15, 2021 @ 05:31
    Carl Sjöholm
    0

    Get current IPublishedContent for Umbraco 9

    I know how to get current content in Umbraco 8 e.g.

    https://our.umbraco.com/forum/umbraco-8/97200-get-current-ipublishedcontent

    But how do I get the same context in Umbraco 9?

    There is no Current.xxx

    I need this in order to handle my custom view models.

    namespace UmbracoProject.Core.ViewModels
    {
    public class LoginViewModel : BaseViewModel
    {
        //  CAN'T Pass null
        public LoginViewModel() : base(Current.UmbracoContext.PublishedRequest.PublishedContent)
        {
    
        }
    
        public LoginViewModel(IPublishedContent content) : base(content)
        {
        }
    
        [Display(ResourceType = typeof(TextResource), Name = "Label_EmailAddress")]
        [Required(ErrorMessageResourceType = typeof(TextResource), ErrorMessageResourceName = "Error_Required")]
        [EmailAddress(ErrorMessageResourceType = typeof(TextResource), ErrorMessageResourceName = "Error_EmailAddress")]
        [DataType(DataType.EmailAddress)]
        public string EmailAddress { get; set; }
    
        [Display(ResourceType = typeof(TextResource), Name = "Label_Password")]
        [Required(ErrorMessageResourceType = typeof(TextResource), ErrorMessageResourceName = "Error_Required")]
        [DataType(DataType.Password)]
        public string Password { get; set; }
    }
    }
    
  • Warren Buckley 2106 posts 4836 karma points MVP ∞ admin hq c-trib
    Oct 20, 2021 @ 11:31
    Warren Buckley
    0

    Hi Carl 👋

    Current is not really a thing anymore in Umbraco V9 and you should use DI to get what you need in the constructor etc...

    As you seem to be doing custom view models I suggest you take a look at this piece of documentation.

    https://our.umbraco.com/documentation.../Reference/routing/custom-controllers#returning-a-view-with-a-custom-model

    So it would be best to use PublishedContentWrapped

    public class LoginViewModel : PublishedContentWrapped
    {
        // The PublishedContentWrapped accepts an IPublishedContent item as a constructor
        public LoginViewModel(IPublishedContent content, IPublishedValueFallback publishedValueFallback) : base(content, publishedValueFallback)
        {
        }
    
    
        [Display(ResourceType = typeof(TextResource), Name = "Label_EmailAddress")]
        [Required(ErrorMessageResourceType = typeof(TextResource), ErrorMessageResourceName = "Error_Required")]
        [EmailAddress(ErrorMessageResourceType = typeof(TextResource), ErrorMessageResourceName = "Error_EmailAddress")]
        [DataType(DataType.EmailAddress)]
        public string EmailAddress { get; set; }
    
        [Display(ResourceType = typeof(TextResource), Name = "Label_Password")]
        [Required(ErrorMessageResourceType = typeof(TextResource), ErrorMessageResourceName = "Error_Required")]
        [DataType(DataType.Password)]
        public string Password { get; set; }
    }
    

    Or alternatively as the documentation extend the partial classes that ModelsBuilder has generated.

    I hope this helps you.

    Warren 😀

  • Carl Sjöholm 44 posts 327 karma points
    Oct 20, 2021 @ 11:59
    Carl Sjöholm
    100

    Hi.

    But the constructor must be empty for it to work with serialization and jquery-validate etc.

    I managed to solve it by inheriting:

    public class BaseViewModel : IContentModel
    

    And setting my own Content.

Please Sign in or register to post replies

Write your reply to:

Draft