Copied to clipboard

Flag this post as spam?

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


  • Harry Spyrou 212 posts 604 karma points
    Aug 22, 2018 @ 15:49
    Harry Spyrou
    0

    Nested Content same aliases with doctypes - Custom Models

    Hello,

    I'm trying to grab some data from Nested Content. I've hijacked stuff:

    using System.Web;
    using Umbraco.Core.Models;
    using Umbraco.Web;
    using Umbraco.Web.Models;
    
    namespace MyModels.Web.Models
    {
        public class CurtainsModuleModel : RenderModel
        {
            public CurtainsModuleModel(IPublishedContent content) : base(content)
            {
            }   
            public IEnumerable<IPublishedContent> GeneralContentCollection => Content.GetPropertyValue<IEnumerable<IPublishedContent>>("generalContentCollection");
    
        }
    }
    

    That will give me the whole collection of Nested Content doctypes I've created. I'm not sure how to access only one of the 20 (or so) nested content doctypes without looping all of them. Is there a way for me to tell the model (or the controller) 'go find that specific doctype inside this main page/doctype and get its value named 'title'.

    Curtains Module is a partial that is part of nested content collection and will have numerous values.

    The only thing I could think of is renaming title to something more specific like 'curtainsTitle' and then looking for the property value.

    TL; DR Don't know how to access a nested content propertyvalue with a generic name "title" in a custom model.

  • Harry Spyrou 212 posts 604 karma points
    Aug 22, 2018 @ 16:59
    Harry Spyrou
    0

    Nobody?

  • Harry Spyrou 212 posts 604 karma points
    Aug 28, 2018 @ 16:38
    Harry Spyrou
    100

    For the future Harry (I'm probably going to look at it again), let me tell to myself that I'm a mediocre developer.

    That being said on to the code:

    I didn't notice that my own main view was passing the item. So here it goes:

    Simplified partial View:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<MyModuleModel>
    @using MyProject.Models;
    
    @{
    //this will give the nested content title with a custom model.
    var title = MyModuleModel.Title
    }
    

    Then the Model which is custom and uses route hijacking:

    using Umbraco.Core.Models;
    using Umbraco.Web;
    using Umbraco.Web.Models;
    
    namespace MyProject.Models
    {
        public class MyModuleModel : RenderModel
        {
    
    public CurtainsModuleModel(IPublishedContent content, IPublishedContent item) : base(content)
            {
                //the model is the actual page (outside the nested content
                //the 'item' here is the nested content item that comes from the main 
                //view loop.
                Title = item.GetPropertyValue<string>("title");
                Summary = item.GetPropertyValue<string>("description");
            }
    
            public string Summary { get; set; }
    
            public string Title { get; set; }
        }
    }
    

    The main view has a loop that renders all the nested content doctypes. Each doctype has a partial of the same name:

    //each item is a nested content doctype that has a partial of the same name
        @foreach (var item in Model.GetPropertyValue<IEnumerable<IPublishedContent>>("generalContentCollection"))
            {
                var partialViewName = "_" + item.DocumentTypeAlias;
    
                    Html.RenderPartial("_myModule", new MyModuleModel(Model, item));
                }
                else
                {
                    @Html.Partial(partialViewName, item);
                }
            }
    
Please Sign in or register to post replies

Write your reply to:

Draft