Copied to clipboard

Flag this post as spam?

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


  • Ray 13 posts 103 karma points notactivated
    Oct 13, 2022 @ 04:05
    Ray
    0

    When building models, the Multinode Treepicker generates different models(Umbraco8.15.5)

    Hello,

    In a Document Type, I created a new Tags field, which uses the Multinode Treepicker type. It allows setting the maximum value of an item. When the maximum value is set to 0, the model generated by this field is as follows,

    enter image description here

    ///<summary>
    /// Tags
    ///</summary>
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder.Embedded", "8.18.5")]
    [ImplementPropertyType("tags")]
    public virtual global::System.Collections.Generic.IEnumerable<global::Umbraco.Core.Models.PublishedContent.IPublishedContent> Tags => this.Value<global::System.Collections.Generic.IEnumerable<global::Umbraco.Core.Models.PublishedContent.IPublishedContent>>("tags");
    

    When the maximum value is set to 1, the model generated by this field is as follows,

    enter image description here

    ///<summary>
    /// Tags
    ///</summary>
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder.Embedded", "8.18.5")]
    [ImplementPropertyType("tags")]
    public virtual global::Umbraco.Core.Models.PublishedContent.IPublishedContent Tags => this.Value<global::Umbraco.Core.Models.PublishedContent.IPublishedContent>("tags");
    

    I also tested in Umbraco 7.15.8. No matter whether the maximum value is set to 0 or 1, the generated model is always a collection type. The model is as follows:,

    ///<summary>
    /// Tags
    ///</summary>
    [ImplementPropertyType("tags")]
    public IEnumerable<IPublishedContent> Tags
    {
        get { return this.GetPropertyValue<IEnumerable<IPublishedContent>>("tags"); }
    }
    

    I don't know why this happens in Umbraco8, because I used this field in some business codes, as shown below

    var testTags = test.Tags;
    

    In this way, whether the maximum value of items is 0 or 1 will affect whether testTags is an object or a collection, thus affecting the execution of subsequent code.

    I don't know how to deal with this situation. Any advice?

    Regards,

    Ray

  • Roy Berris 89 posts 577 karma points c-trib
    Oct 13, 2022 @ 10:44
    Roy Berris
    0

    Hi Ray,

    This is intented behaviour. I am not sure what the question is. It is set for a doctype, and this is a constant, unless you change it manually. You can just change your code to match a object instead of a collection.

    If a lot of code depends on a collection you can just create an array from the single object?

    var testTags = new[] { test.Tags };
    

    Making an IPublishedContent[] which is an IEnumerable<IPublishedContent>.

  • Ray 13 posts 103 karma points notactivated
    Oct 14, 2022 @ 03:37
    Ray
    0

    Hi Roy,

    Thank you very much. I conducted a new test,

    Create a new Document Type named HomePage, which has a Tags field. The value of Maximum number of items is set to 0,

    enter image description here enter image description here enter image description here

    and then add the model file generated by Models Builder to the project(Note when the value of Maximum number of items is set to 0, the type of tags in the corresponding model is IEnumerable<>),

    enter image description here

    ///<summary>
    /// Tags
    ///</summary>
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder.Embedded", "8.18.5")]
    [ImplementPropertyType("tags")]
    public virtual global::System.Collections.Generic.IEnumerable<global::Umbraco.Core.Models.PublishedContent.IPublishedContent> Tags => this.Value<global::System.Collections.Generic.IEnumerable<global::Umbraco.Core.Models.PublishedContent.IPublishedContent>>("tags");
    

    Then get the tags data through the code as follows,

    var content = Current.PublishedContentQuery.ContentAtRoot();
    var homePageNode = content.Where(x => x.ContentType.Alias.ToLower() == HomePage.ModelTypeAlias.ToLower()).OfType<HomePage>().FirstOrDefault();
    var tags = homePageNode.Tags;
    

    Now we can get two pieces of data for tags, which seems normal.

    The consequence is that when the user changes the value of Maximum number of items to 1 from 0 in CMS, the data of tags obtained by the above code is null.

    I guess this problem may be caused by the inconsistency between the CMS settings and the Model class, because the value of Maximum number of items is set to 1 from 0 in Umbraco8, which will affect the model class to generate different codes, as follows,

    ///<summary>
    /// Tags
    ///</summary>
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder.Embedded", "8.18.5")]
    [ImplementPropertyType("tags")]
    public virtual global::Umbraco.Core.Models.PublishedContent.IPublishedContent Tags => this.Value<global::Umbraco.Core.Models.PublishedContent.IPublishedContent>("tags");
    

    We can't force the server to update the Model class again because the user has modified the value of Maximum number of items in CMS. I think this may be a disaster.

    In Umbraco7, no matter the value of Maximum number of items is set to 0 or 1, the generated model classes are always consistent, tags field is always an IEnumerable list.

    My question is that if the user modifies the value of Maximum number of items in the tags field of CMS, some business codes will not work properly. If the Model class in the project needs to be updated again, I think this may be a disaster, any advice?

    Thank you

Please Sign in or register to post replies

Write your reply to:

Draft