Copied to clipboard

Flag this post as spam?

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


  • Ryan Carlin 1 post 71 karma points
    Dec 04, 2019 @ 16:29
    Ryan Carlin
    0

    Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent

    I have the below surface controller that has an Ajax call from the UI but when it tries to instantiate the SelectViewModel() it is giving me an error with the UmbracoHelper.

    "System.InvalidOperationException: 'Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent.'"

    Am I supposed to instantiate the UmbracoHelper again or can I not create ViewModelObjects within an Ajax call? Any help appreciated.

    public class SelectSurfaceController : BaseSurfaceController
    {
        [HttpGet]
        public ActionResult TravellersTemplate(int count)
        {
            var model = new SelectViewModel();
            model.TravellersDatesOfBirth = Enumerable.Range(1, count).Select(x => new DateTime()).ToList();
    
            return PartialView("_Travellers", model);
        }
    
    
    
    public class SelectViewModel : LayoutViewModel
    {
        public SelectViewModel() : base(Current.UmbracoHelper.AssignedContentItem) { }        
        public SelectViewModel(IPublishedContent content) : base(content) { }
    
  • Mike Ryan 5 posts 75 karma points
    Oct 15, 2021 @ 02:11
    Mike Ryan
    0

    Did you ever figure this one out Ryan? I was trying to do nearly the same thing, and I'm sure quite a few other people too. They discuss it here and link to some documentation. I personally just created an ApiClient that I could call from a template view and returned a custom ViewModel I created from Shopify product info, completely outside of the Umbraco IPublishedContentModels. A bit of overengineering in my opinion, but I don't pretend to be an expert.

  • ewuski 19 posts 119 karma points
    22 days ago
    ewuski
    0

    I have the same issue in Umbraco 10 with the following simple line in a surface controller:

    var aaa = _umbracoHelper.AssignedContentItem;
    

    where _umbracoHelper was constructed with the DI.

    However SurfaceController has a property CurrentPage. I have just assigned it to my model in the controller.

Please Sign in or register to post replies

Write your reply to:

Draft