Question: How to programmatically add item to BagPart #6927
-
I have a ContentType ActivitiesPage which has a BagPart in it. In the BagPart I have multiple entries of the type ActivityDateItem. I have created a ActivityDateItem.cs model
I load the ActivitiesPage through the following call After this, I try to add an item as following
This all goes through without any exception. But when calling again Thanks for your help |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 2 replies
-
This looks like something for Discussions so I'll convert it into a discussion. |
Beta Was this translation helpful? Give feedback.
-
There's a few things here which don't seem right.
Something like this makes more sense.
|
Beta Was this translation helpful? Give feedback.
-
thank you @deanmarcussen - this input was very helpful. So I can now add a new item into the list, this one works. What I saw when using NewAsync method I get the ContentType, but it contains (when checking the json in the debugger) only the contained parts, but not the fields. It's the same behavior when using ContentTypes from the built-in examples. So what happnes is in the second line of your code when casting to ActivityDateItem I get the following error: As shown in my sample code, ActivityDateItem extends ContentPart - not sure if this is the issue. Thanks for helping |
Beta Was this translation helpful? Give feedback.
-
Yes, the fields aren't welded on till later, and it won't become an Try with
While hard type parts seem like a useful thing, they were never designed to be that, (they work because of a side effect), so there are a bunch of things that don't necessarily behave how you would expect, and places where you want to use them, but you can't, because they haven't become that yet. As much as I like my hard typed classes, I've tried them, and gone away from them for the most part, as some of the ways their behaviour changes, in particular with display management (they are no longer placed in a dynamic part shape, which I find useful) |
Beta Was this translation helpful? Give feedback.
-
Thanks for the details. The data is now saved, but with a little issue that the ContentPart is dded next to the ActivityItem. From the Debugger
The item which has been generated within Orchard looks like this.
Just to be sure. You are saying that Typed Classed are not supported (That's ok for me), but it is allowed to use the APIs to add new entries programmatically, right? |
Beta Was this translation helpful? Give feedback.
-
Thanks very much, now all works as expected. Is this something you would like I make a entry into the how-to guides? https://docs.orchardcore.net/en/dev/docs/guides/ |
Beta Was this translation helpful? Give feedback.
Yes, the fields aren't welded on till later, and it won't become an
ActivityDateItem
(Part) until it gets serialized there and back again, probably.Try with
While hard type parts seem like a useful thing, they were never designed to be that, (they work because of a side effect), so there are a bunch of things that don't necessarily behave how you would expect, and places where you want to use them, but you can't, because they haven't become that yet.
As much as I like my hard typed classes, I've tried them, and gone away from them for the most part, as som…