Copied to clipboard

Flag this post as spam?

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


  • Jo Mixon 13 posts 83 karma points
    Apr 20, 2021 @ 15:27
    Jo Mixon
    0

    Custom Surface Controller V8.12

    Hello and Thank you ahead of time,

    My problem up front is not being able to figure out a way to grab an instance of IPublishedContent I think. I get the "No parameterless constructor defined for this object" after I set everything up the way I think it should be. I have followed all of the tutorials, but maybe I'm doing something wrong. To actually get something up and running I simply take out the HeadNavigation and FooterNavigation but this isn't feasible going forward.

    Also, it could very well be the setup. I created a Controllers Folder as would be done in a typical MVC project and a Views folder where I can scaffold empty views by default.

    I can get to the pages and pull back the custom data I am grabbing when I go to the /umbraco/surface/Controller/View, again only if I remove Header and Footer Navigation that is being populated via the backoffice.

    To provide some code examples:

    MODEL

    {

    namespace Umbraco.ViewModels

    {

    public class PolicyViewModel : PublishedContentWrapped
    
    {
    
        public PolicyViewModel(IPublishedContent content) : base(content) { }
    

    }}}

    VIEW

    @model Umbraco.ViewModels.PolicyViewModel

    @{ ViewBag.Title = "Index";

    }

    Index

    ........

    CONTROLLER

    namespace Umbraco.Controllers

    { public class PolicyController : SurfaceController{

    public async Task

    PolicyViewModel policyView = new PolicyViewModel(model.Content);

    return View(policyView);

    Is there something I'm missing? Set all of this up wrong? Or just missing some sort of initializer or something to get this wired up?

    Again, appreciate any insight and a very big thanks as always.

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Apr 20, 2021 @ 19:45
    Huw Reddick
    1

    You should be injecting the interface into the constructor of your surface controller not your view model.

  • Jo Mixon 13 posts 83 karma points
    Apr 21, 2021 @ 14:54
    Jo Mixon
    0

    Thanks for the response.

    So, I have now included this:

    public class PolicyController : SurfaceController
    {
        public PolicyController(IPublishedContent content) : base() { }
    

    And now receive this error:

    Unresolved dependency [Target Type: Umbraco.Controllers.PolicyController], [Parameter: content(Umbraco.Core.Models.PublishedContent.IPublishedContent)], [Requested dependency: ServiceType:Umbraco.Core.Models.PublishedContent.IPublishedContent, ServiceName:]

    Is there a specific Service from Umbraco I'm supposed to be using here? Completely at a loss on where to go from here.

    Thanks again for your time.

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Apr 21, 2021 @ 17:24
    Huw Reddick
    0

    Not sure exactly what you are attempting as your code is a but mashed up, but you can inject the Ipublishedcontenet into your view Model like so

     public class ResultViewModel : PublishedContentWrapped
        {
            // The PublishedContentWrapped accepts an IPublishedContent item as a constructor
            public ResultViewModel(IPublishedContent content) : base(content) { }
    
            // Custom properties here...
            public string Guid { get; set; }
            public Installer Business { get; set; }
        }
    

    To access publishedcontent in your controller you just need to use

    IPublishedContent homePage = Umbraco.Content(Guid.Parse("a53c075a-61a2-456e-a73d-e65e52efea92"));
    

    Perhaps you could explain what you are trying to achieve

  • Jo Mixon 13 posts 83 karma points
    Apr 21, 2021 @ 19:57
    Jo Mixon
    0

    Thanks for taking the time to answer.

    I do have that injection in the View Model. Was told to put it in the Controller so I did that and that's when I got a different error.

    I did use your method of passing in my hompage as the IPublishContent to the controller. I still can't seem to access anything in my View.

    My issue and what I need to do is to use the master.cshtml in my custom Views and they use things like Model.Root() pull our navigation in the Header and Footer from code like the following: FOOTER @inherits Umbraco.Web.Mvc.UmbracoViewPage

     <ul class="footer-nav-items" aria-label="Menu" role="menubar">
            @{
                var homeChildNavSelection = Umbraco.Content(Guid.Parse("fbeb2b-77cc-4286-9ac0-d3de4c749b"))
                .Children()
                .Where(x => x.Value<bool>("footerNavItem") == true);
            }
            @foreach (var item in homeChildNavSelection)
            {
                <li role="none">
                    @if(item.Name == "Calendar"){
                        <a href="@item.Url?pageId=1484" role="menuitem">> @item.Name</a>
                    } else if(item.Name == "Forms"){ 
                        <a href="@item.Url?pageId=1174" role="menuitem">> @item.Name</a>
                    }
                    else {
                        <a href="@item.Url" role="menuitem">> @item.Name</a>
                    }
                </li>
    
            }  
        </ul>
    

    HEADER

    @inherits Umbraco.Web.Mvc.UmbracoViewPage

    @using Umbraco.Web;

    @{

    var site = Model.Root();
    var selection = site.Children.Where(x => x.IsVisible());
    var global = Umbraco.Content(Guid.Parse("5c0ef-56b5-4884-a7e-56101c9bde"));
    

    }

            <img src="@(global.Value<IPublishedContent>("headerLogoColor").Url)" alt="" />
    

    Overall I guess would it be possible to get access to some of these values and if so, how would I access them through the View? Or how would you access that homePage via the View? Guessing I can send in the siteRoot via something like that and see if I can use that.

    Thanks again for everything!

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Apr 21, 2021 @ 20:13
    Huw Reddick
    0

    Still not quite sure what you are trying to accomplish. If you declare a variable in one view you can't access that variable in another view unless you use tempests or viewbag variables, jusetting var variable = value gives you a local scoped variable for that view.

    What exactly do you want to access in your view? If it is a property in your site home page then you can get that easily enough but please explain more about what you are trying to do. I'm still not sure exactly what you need.

  • Jo Mixon 13 posts 83 karma points
    Apr 21, 2021 @ 20:21
    Jo Mixon
    0

    Thank you for the quick response and I'll try to provide the same.

    In my views from the surface controllers I have a master.cshtml Layout.

    This Layout page is what I am using on every page and want to use. This Layout page also, pulls it's navigation from what is changed in the backoffice of the Umbraco site.

    When I attempt to load this master it has a reference to partial pages that themselves use local variables. These are Null when loading the surface controller pages and I would like to gain access to these so that I can use this master Layout on all of my pages going forward. I think that is where I am most confused is if I can use a master page in these surface controller pages.

  • Jo Mixon 13 posts 83 karma points
    Apr 21, 2021 @ 20:33
    Jo Mixon
    0

    It may also be my setup that's not allowing me access because I just tried with passing that HomePage through a ViewBag and it was still null.

    So, for brief explanation of how I set up a Surface Controller.

    I created a Controllers Folder/Create Controller and inherit from : SurfaceController

    I have a ViewModel that inherits from : PublishedContentWrapped and I inject in that ViewModel the IPublishedContent like below:

    public PolicyViewModel(IPublishedContent content) : base(content) { }

    From my Controller now, I can just: IPublishedContent homePage = Umbraco.Content(Guid.Parse("4590ef-56b5-4884-a87e-561c99bded")); ViewBag.HomePage = homePage;

    And now I should have access to those properties from that DocType? Or is there another step that I am missing?

    I feel like I'm close, just either something isn't set up right or I'm trying to do something I'm either not understanding or just plain doing wrong.

  • Jo Mixon 13 posts 83 karma points
    Apr 21, 2021 @ 21:18
    Jo Mixon
    0

    On top of this note and reading more on this topic: Does anything have to be setup in the backoffice for any of this to work? That is something that has not been done.

    I just assumed I could download Nuget Umbraco, install it and create the necessary folders for the controllers and views (as I have done and route there with /umbraco/surface/Controller/Page. Now when trying to load the built in Master page with navigation is where my issues come in. Can I get access to any of the navigation elements or is that a different or special setup that must go through the backoffice?

    Forgive my ignorance.

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Apr 22, 2021 @ 10:37
    Huw Reddick
    0

    Hi Jo,

    Just a few questions if you don't mind so I can figure out where in the process you are and what you have done.

    I assume after you installed via nuget hat you opened the website to run the configuration which will have installed the default starter kit, is that correct? (I'm assuming it is since you have a default master page)

    What do you mean by 'Now when trying to load the built in Master page with navigation' ? you wouldn't normally load the masterpage it is just a template for the layout of other pages.

  • Jo Mixon 13 posts 83 karma points
    Apr 22, 2021 @ 13:21
    Jo Mixon
    0

    Sorry Huw,

    I did use bad terminology, by load I simply mean using it as a layout. I commented out all of the code that was Umbraco specific to get the page to display. Specifically removed the header and footer navigation as stated above.

    At this point in time, I'm just going to assume I am not allowed to do what I am trying since all I get is null values when attempted.

    Since I can create the controllers and views and can pull them up from /umbraco/surface/Controller/View links then I can re-write what I have via the back office and use Ajax to gather the information to display on screen. I preferred not to go this route, but think it's my only option at this point in time.

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Apr 22, 2021 @ 18:38
    Huw Reddick
    0

    Possibly you just need to do it differently, perhaps you could post the code for a simple view and I can point you in the right direction to achieve what you want.

  • Jo Mixon 13 posts 83 karma points
    Apr 22, 2021 @ 19:12
    Jo Mixon
    0

    Thank you very much! I really mean that.

    I'll continue down the path of just writing JsonResult Controllers and use Ajax within the backoffice pages at this time. This allows the clients to be able to make their own modifications if they ever have the need.

    If there are any specific resources to this issue I would be happy to take a look at them because there may be a need in the future. I feel like I have attempted most of what I have found, but none were step by step so I probably just missed a valuable step or just didn't understand before starting down this path.

Please Sign in or register to post replies

Write your reply to:

Draft