Unable to retrieve Field value in code

Topics: Customizing Orchard
Nov 5, 2013 at 8:51 AM
I've tried different ways of doing this, but still I'm not able to retrieve a custom field's value if the field is directly under the content type.

Product is custom content type
ModelName is field name

ContentItem.Product.ModelName.Value doesn't work, returns 'Orchard.ContentManagement.ContentItem' does not contain a definition for 'ModelName'

What is the correct way to do this? I have a feeling I'm missing something easy here.

P.S.
What I was able to do to get it working was to place the field inside a custom part, for example, a ProductPart. Then ContentItem.ProductPart.ModelName.Value works. I'm sure there is a way to do it without this workaround.

Thanks :)
Coordinator
Nov 6, 2013 at 7:10 AM
A field is never directly under a content type, no matter what the admin makes it look like. In reality, a part with the same name as the type is created, to which the fields are added.
Marked as answer by umamibrother on 11/18/2013 at 6:25 AM
Nov 6, 2013 at 7:32 AM
Thanks for the reply Bertrand. So the part's name will be 'Product' and so ContentItem.Product.ModelName.Value should work? In my case it did not work, how can I refer to this part that has been created?

Thanks!
Coordinator
Nov 6, 2013 at 7:43 AM
Yes, that should work. How does it not?
Nov 7, 2013 at 12:17 AM
Edited Nov 7, 2013 at 2:29 AM
That's the part that confounds me. I get the following:
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
'Orchard.ContentManagement.ContentPart' does not contain a definition for 'ModelName'
</ExceptionMessage>
<ExceptionType>
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
</ExceptionType>
<StackTrace>
at CallSite.Target(Closure , CallSite , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) at CarrierApi.Controllers.CarrierApiController.Get() at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<>c__DisplayClass5.<ExecuteAsync>b__4() at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)
</StackTrace>
</Error>
Using 1.71, I'm running this code from a custom module. This module exposes content items vi Web API.
Coordinator
Nov 7, 2013 at 7:00 AM
First cast the content item to dynamic.
Nov 7, 2013 at 7:57 AM
Edited Nov 7, 2013 at 8:00 AM
Content item is already cast to dynamic.

Quoting this part from my original message:
P.S.
What I was able to do to get it working was to place the field inside a custom part, for example, a ProductPart. Then ContentItem.ProductPart.ModelName.Value works. I'm sure there is a way to do it without this workaround.
Coordinator
Nov 11, 2013 at 8:10 AM
I don't know what you're doing differently, but dynamic dyncontentItem = contentItem; var field = contentItem.Product.ModelName does definitely work.