Copied to clipboard

Flag this post as spam?

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

  • Craig 34 posts 270 karma points
    Jul 24, 2019 @ 11:57

    ContentModel View Model Form Post


    I'm trying to perform an ajax post from a form to a controller in v8 of Umbraco. Previously in v7 my view model would inherit from RenderModel and this model would be passed to my controller Action method meaning I had access to my View Model's custom properties.

    I've noted from the documentation for v8 ( that my view model should now inherit from ContentModel so I've done that and got the form to render. The problem comes when I want to post that form back to the Action method. As I'm passing an object of type ContentModel (as opposed to my custom ViewModel) into the Action method I don't have access to a couple of properties I need but if I change the Action method to accept my custom View Model the code complains that I don't have a parameterless constructor in my custom View Model.

    In v7 this would be overcome with a class definition like:

        public NewsAreaViewModel(IPublishedContent content) : base(content) { NewsPages = new ContentListViewModel(); }
        public NewsAreaViewModel() : this(new UmbracoHelper(UmbracoContext.Current).TypedContent("1")) { NewsPages = new ContentListViewModel(); }
        public NewsAreaViewModel(IPublishedContent content, CultureInfo culture) : base(content, culture) { NewsPages = new ContentListViewModel(); }

    But I can't figure out how to do this in v8 as I can't use new UmbracoHelper within the constructor...

    Any ideas?

  • Craig 34 posts 270 karma points
    Jul 24, 2019 @ 13:58

    Okay so I got this working by switching my ViewModel class up as follows:

        private static IPublishedContent _blogPage
            get {
                var helper = Umbraco.Web.Composing.Current.UmbracoHelper;
                return helper.Content(System.Configuration.ConfigurationManager.AppSettings["BlogPageId"].ToString());
        // Standard Model Pass Through
        public BlogViewModel(IPublishedContent content) : base(content) { BlogPages = new BlogPageViewModel(); }
        public BlogViewModel() : this(_blogPage) { }   

    However I read on another post that using Umbraco.Web.Composing.Current.UmbracoHelper wasn't best practice but it didn't say why. I appreciate that when working in a controller I get access to UmbracoHelper via Umbraco. but I can't see another way of getting access to it via a class file so any help on this would be appreciated.

  • Markus Johansson 1761 posts 5162 karma points c-trib
    Oct 22, 2019 @ 08:37
    Markus Johansson


    Are you putting the POST-Action on the "hijack"-controller and posting to this using AJAX?

    It might be better to use a Surface Controller in this scenario with a model that is only used for you form - avoid having to pass the whole model back to the end point. I often do this to get fallback when no javascript is activated, we then enrich the form with AJAX posting.

    If you're only demand is to post to an action using AJAX (and you do not need any support without javascript) you could also create a ApiController (inherit from UmbracoApiController).

    If you need access to properties on the current page you could pass the id of the current page into your controller action and fetch it from the cache using the exposed properties on the base UmbracoApiController.

Please Sign in or register to post replies

Write your reply to: