New content item id returned when validation fails

Topics: Core, Customizing Orchard, Troubleshooting
Oct 28, 2013 at 1:24 PM
I have a content part and in the editor template for this part I have some specific text that needs to be shown only when a new content item is being created (not when an existing item is updated).

To achieve this I wrap the text in a conditional statement so it's only show if the value of Model.ContentItem.Id == 0.

In general this works fine. When creating a new item, and my part is rendered, the content item id is 0, so the conditional text is displayed. When editing an existing item, the content item id contains a non-zero value, so the conditional text in my part template is not displayed.

However, my part also has some fields that have validation. I've noticed that when creating a new content item, at first the conditional text is displayed because the Model.ContentItem.Id is zero. However if I attempt to save the new content item and validation fails (eg. I leave a required field blank). Then when I'm returned to the editor template, the Model.ContentItem.Id now has a value (the next identity value for the content item), even though the transaction has rolled back and no content item with that id has actually been persisted. Thus, the conditional text in my editor template is not displayed when it should still be. Each time I hit the save button and the validation fails, the Model.ContentItem.Id value returned in the view increments (even though no content item is being persisted).

Is this a bug? Should the content manager ensure the underlying ContentItem.Id is returned to an empty state if a new content item fails to be created? Otherwise how is the best way to tell detect within an editor template for a part if the item is new vs existing (even after validation fails)?
Nov 3, 2013 at 3:38 PM
I just had the same problem and used the following code to detect if the content item is new

itemIsNew = (Model.ContentItem.VersionRecord == null || !Model.ContentItem.VersionRecord.Published);

and I'm now also wondering if this is the right / recommended / best approach to distinguish between a new and an updated content item.