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,
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?
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>.
Create a new Document Type named HomePage, which has a Tags field. The value of Maximum number of items is set to 0,
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<>),
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,
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?
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,
When the maximum value is set to 1, the model generated by this field is as follows,
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:,
I don't know why this happens in Umbraco8, because I used this field in some business codes, as shown below
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
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?
Making an
IPublishedContent[]
which is anIEnumerable<IPublishedContent>
.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,
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<>),
Then get the tags data through the code as follows,
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,
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
is working on a reply...