TextField value not getting saved

Topics: Troubleshooting
Nov 28, 2012 at 1:20 AM

I'm trying to create a content item and set the value on a text field.  The TitlePart and BodyPart values are getting saved but the 2 text fields are not getting saved and I'm not getting any errors.  Am I doing something wrong?

From my migration.cs file

ContentDefinitionManager.AlterPartDefinition("Product", builder =>
        .WithField("ProductImageUrl", fld =>
        .WithField("InternalId", fld =>

ContentDefinitionManager.AlterTypeDefinition("Product", builder =>

When I try to create a content item with the code below, the ProductImageUrl and InternalId values do not get saved

dynamic product = _contentManager.New("Product");
product.TitlePart.Title = "Title";
product.BodyPart.Text = "Body";
product.Product.ProductImageUrl.Value = "image url";
product.Product.InternalId.Value = "custom ID";

_contentManager.Create(product, VersionOptions.Published);

When I debug the code it appears to set the value but then when I view the new content item, the 2 text fields are blank.

Nov 28, 2012 at 6:19 AM

Attach a debugger, set a breakpoint in the field property's setter. I'm suspecting it's never hit. Using dynamic has its pitfalls, one being that if you give it a non pre-existing path, it won't complain and will just store your data on an object created on the fly but that is not what you intended.

Nov 28, 2012 at 5:15 PM

Maybe I'm missing something.  Is it possible to create a content item in a module for a content type that was created through Orchard admin?  I couldn't find any examples.

When I set a break in the code above and look at the product object it appears to be the proper type and the value's on the textfields get set but when the content item is created and I look at it in the Orchard admin, only the body and title are set.

-		product	{Orchard.ContentManagement.ContentItem}	dynamic {Orchard.ContentManagement.ContentItem}
	+		_behavior	Count = 1	ClaySharp.IClayBehavior {ClaySharp.ClayBehaviorCollection}
	+		_parts	Count = 8	System.Collections.Generic.IList {System.Collections.Generic.List}
	+		ContentManager	{Orchard.ContentManagement.DefaultContentManager}	Orchard.ContentManagement.IContentManager {Orchard.ContentManagement.DefaultContentManager}
			ContentType	"Product"	string
			Id	0	int
	+		Orchard.ContentManagement.IContent.ContentItem	{Orchard.ContentManagement.ContentItem}	Orchard.ContentManagement.ContentItem
	+		Orchard.ContentManagement.IContentBehavior.Behavior	Count = 1	ClaySharp.IClayBehavior {ClaySharp.ClayBehaviorCollection}
	-		Parts	Count = 8	System.Collections.Generic.IEnumerable {System.Collections.Generic.List}
		+		[0]	{Orchard.ContentManagement.ContentPart}	Orchard.ContentManagement.ContentPart {Orchard.ContentManagement.ContentPart}
		+		[1]	{Orchard.Projections.Models.FieldIndexPart}	Orchard.ContentManagement.ContentPart {Orchard.Projections.Models.FieldIndexPart}
		+		[2]	{Orchard.Core.Common.Models.CommonPart}	Orchard.ContentManagement.ContentPart {Orchard.Core.Common.Models.CommonPart}
		+		[3]	{Orchard.Core.Title.Models.TitlePart}	Orchard.ContentManagement.ContentPart {Orchard.Core.Title.Models.TitlePart}
		+		[4]	{Orchard.Autoroute.Models.AutoroutePart}	Orchard.ContentManagement.ContentPart {Orchard.Autoroute.Models.AutoroutePart}
		+		[5]	{Orchard.Core.Common.Models.BodyPart}	Orchard.ContentManagement.ContentPart {Orchard.Core.Common.Models.BodyPart}
		-		[6]	{Orchard.ContentManagement.ContentPart}	Orchard.ContentManagement.ContentPart
			+		_behavior	Count = 1	ClaySharp.IClayBehavior {ClaySharp.ClayBehaviorCollection}
			+		_fields	Count = 2	System.Collections.Generic.IList {System.Collections.Generic.List}
			+		_zones	{Orchard.UI.ZoneCollection}	Orchard.UI.IZoneCollection {Orchard.UI.ZoneCollection}
			+		ContentItem	{Orchard.ContentManagement.ContentItem}	Orchard.ContentManagement.ContentItem
			-		Fields	Count = 2	System.Collections.Generic.IEnumerable {System.Collections.Generic.List}
				-		[0]	{Orchard.Core.Common.Fields.TextField}	Orchard.ContentManagement.ContentField {Orchard.Core.Common.Fields.TextField}
					-		[Orchard.Core.Common.Fields.TextField]	{Orchard.Core.Common.Fields.TextField}	Orchard.Core.Common.Fields.TextField
						+		base	{Orchard.Core.Common.Fields.TextField}	Orchard.ContentManagement.ContentField {Orchard.Core.Common.Fields.TextField}
								Value	"image url"	string
							DisplayName	"Product Image Url"	string
					+		FieldDefinition	{Orchard.ContentManagement.MetaData.Models.ContentFieldDefinition}	Orchard.ContentManagement.MetaData.Models.ContentFieldDefinition
							Name	"ProductImageUrl"	string
					+		PartFieldDefinition	{Orchard.ContentManagement.MetaData.Models.ContentPartFieldDefinition}	Orchard.ContentManagement.MetaData.Models.ContentPartFieldDefinition
					+		Storage	{Orchard.ContentManagement.FieldStorage.FieldStorageEventStorage}	Orchard.ContentManagement.FieldStorage.IFieldStorage {Orchard.ContentManagement.FieldStorage.FieldStorageEventStorage}
				+		[1]	{Orchard.Core.Common.Fields.TextField}	Orchard.ContentManagement.ContentField {Orchard.Core.Common.Fields.TextField}
				+		Raw View		
					Id	0	int
			+		Orchard.ContentManagement.IContentBehavior.Behavior	Count = 1	ClaySharp.IClayBehavior {ClaySharp.ClayBehaviorCollection}
			+		PartDefinition	{Orchard.ContentManagement.MetaData.Models.ContentPartDefinition}	Orchard.ContentManagement.MetaData.Models.ContentPartDefinition
			+		Settings	Count = 0	Orchard.ContentManagement.MetaData.Models.SettingsDictionary
			+		TypeDefinition	{Orchard.ContentManagement.MetaData.Models.ContentTypeDefinition}	Orchard.ContentManagement.MetaData.Models.ContentTypeDefinition
			+		TypePartDefinition	{Orchard.ContentManagement.MetaData.Models.ContentTypePartDefinition}	Orchard.ContentManagement.MetaData.Models.ContentTypePartDefinition
			+		Zones	{Orchard.UI.ZoneCollection}	Orchard.UI.IZoneCollection {Orchard.UI.ZoneCollection}
			+		Dynamic View	Expanding the Dynamic View will get the dynamic members for the object	
		+		[7]	{Orchard.ContentManagement.FieldStorage.InfosetStorage.InfosetPart}	Orchard.ContentManagement.ContentPart {Orchard.ContentManagement.FieldStorage.InfosetStorage.InfosetPart}
		+		Raw View		
			Record	null	Orchard.ContentManagement.Records.ContentItemRecord
	+		TypeDefinition	{Orchard.ContentManagement.MetaData.Models.ContentTypeDefinition}	Orchard.ContentManagement.MetaData.Models.ContentTypeDefinition
			Version	0	int
			VersionRecord	null	Orchard.ContentManagement.Records.ContentItemVersionRecord
	+		Dynamic View	Expanding the Dynamic View will get the dynamic members for the object	

Nov 28, 2012 at 8:51 PM

What may be lacking here is the ability to dynamically get at the content part if it was created from the admin. When you evaluate product.Product in the debugger, what do you see? In particular, what behaviors do you see?

As a workaround, you can get to the fields like this: part.ContentItem.Parts.Select(p => p.Fields.First(f => f.Name == "TheNameOfTheField")).First()

Nov 29, 2012 at 1:31 AM

I noticed that the content items that were getting created had the values of the text fields stored in the database table Orchard_Framework_ContentItemRecord in the data column as

<Data><Product><ProductImageUrl>image url</ProductImageUrl><InternalId>custom ID</InternalId></Product></Data>

but the values did not show on the content item in the admin UI (only the title and body fields were populated).

Does this give you any clue as to what is happening?

I tried your workaround and couldn't get it to work.  It said the field didn't exist even though when I look under the debugger the 2 textfields show up under the ContentPart. (Is it under the ContentPart because the part is the same name as the type?)

I might just give up trying to use this approach as I thought it would have made what I was trying to do easier but doesn't seem to work at all!

Nov 29, 2012 at 6:15 PM

It should definitely work. I really don't understand why it doesn't in your case. I would investigate further in the debugger. you might want to try to look at part.ContentItem.Parts.SelectMany(p => p.Fields). That should give you a flat list of all fields on all parts to examine. That may give you some additional clues.