Copied to clipboard

Flag this post as spam?

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


  • Sam 23 posts 115 karma points
    Mar 05, 2020 @ 14:19
    Sam
    0

    Extending Umbraco with custom controller and routing

    Hello,

    When i call a partial it complains the Model isn't IPublishedContent?

    But when i tell my custom ViewModel to inherit from IPublishedContent like in the documentation, i get the following error:

    Err: 'No parameterless constructor defined for this object.'

    How do i fix this so i can use my partials?

    Code below:

    ViewModel:

    public class MainViewModel : ContentModel
    {
        //Constructor of MainViewModel
        public MainViewModel(IPublishedContent content) : base(content)
        {
            SiteSettings = new SiteSettingsViewModel();
            BusinessCentersData = new List<BusinessCenterDataViewModel>();
            BusinessCenterData = new BusinessCenterDataViewModel();
            SiteSettings = new SiteSettingsViewModel();
            UmbracoBusinessCenters = new UmbracoBusinessCentersViewModel();
            RawContent = RawContent;
            }
    
        //Independant View 
        public SiteSettingsViewModel SiteSettings { get; set; }
        public UmbracoBusinessCentersViewModel UmbracoBusinessCenters { get; set; }
        public List<BusinessCenterDataViewModel> BusinessCentersData { get; set; }
        public BusinessCenterDataViewModel BusinessCenterData { get; set; }
        public IPublishedContent RawContent {get; private set; }
    
        public string siteName { get; set; }
        public dynamic RootNode { get; set; }
    }
    

    Controller:

    public ActionResult BusinessCenters(MainViewModel model)
        {
            try
            {
    
                using (var cref = _context.EnsureUmbracoContext())
                {
                    //Content Cache
                    //var cache = cref.UmbracoContext.Content;
                    var cache = cref.UmbracoContext.Content;
                    dynamic rootNode = cache.GetAtRoot();
                    model.RootNode = rootNode;
                    //Call Any Missing Umbraco Content
                    IPublishedContent siteSettings = cache.GetSingleByXPath("//siteSettings");
                    UmbracoContentBuilderClass siteSettingsModel = new UmbracoContentBuilderClass();
                    model.SiteSettings = siteSettingsModel.PopulateSiteSettingsModel(siteSettings);
    
                    IPublishedContent businessCenters = cache.GetSingleByXPath("//businessCenters");
                    UmbracoContentBuilderClass businessCentersModel = new UmbracoContentBuilderClass();
                    model.UmbracoBusinessCenters = businessCentersModel.PopulateBusinessCentersModel(businessCenters);
    
    
    
                }
                    return PartialView("~/Views/thejourney/firstpage.cshtml", model);
    
    
            }
            catch (Exception e)
            {
                CustomLogger.Error($"ERROR: BusinessCenterListingsController.BusinessCenters (GET) {e}");
                throw;
            }
    

    Partial:

    @inherits UmbracoViewPage<officefreedom.AppCode.ViewModels.MainViewModel>
    <div class="upper-nav-container col-xs-12 col-md-12">
    @Html.Partial("~/Views/Partials/_Navigation.cshtml", Model.RawContent)
    

Please Sign in or register to post replies

Write your reply to:

Draft