This project is read-only.

retrieve 'TextField' programmatically

Topics: General
Nov 21, 2013 at 3:29 PM
I have a ContentType and ContentItem defined as follows:

ContentType: 'MyType'
Parts included: common, autoroute, title
Fields: 'MyField' as a text field (text area)
ContentItem: 'myContentItem'

Now I would like to retrieve the 'value' of this field with something like:
@{string text = Model.myContentItem.ThePartThatHasTheField.MyField.Value;}

In a thread I learned to provide below info:

However, I cannot figure out what is my 'ThePartThatHasTheField'.
  • what/where is the part containing my field?
  • am I on the right track at all?
thanks for your time and advise
Nov 21, 2013 at 10:44 PM
If you attached the field to your content type, Orchard will have created a content part with the exact same name as your type, and attached the field to that part.
So the expression would be:
var textFieldValue = (string)someContentItem.MyType.MyField.Value;
Nov 22, 2013 at 9:08 AM
Edited Nov 24, 2013 at 5:16 PM
thanks, in fact .... I was once there!?
I must have a blackout: in below code 'text' is always NULL! I really double checked the naming/spelling!

Also: MyTypeItem renders MyField correctly. Now I would like to load this same text (MyField.Value) into my accordion.
(Clearly this is a test i.e. the setup does not make sense!)

what in the world I'm missing! @using .... ?
thanks for your time

....\TheThemeMachine\Views\Content-MyType.cshtml (alternate):
@using Orchard.Utility.Extensions;
    if (Model.Title != null) {
        Layout.Title = Model.Title;
       var contentTypeClassName = ((string)Model.ContentItem.ContentType).HtmlClassify();

@*  my code *@
       dynamic MyTypeItem = Model.MyTypeItem;
    string text = MyTypeItem.MyType.MyField.Value;
@*  end my code *@ 
<article class="content-item @contentTypeClassName">
        @if (Model.Meta != null) {
        <div class="metadata">
@*  start of my accordion code*@        
                <ul id="accordion">
                <h2>Title One</h2>
                <div class="content">
                    Quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
                    consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse.
                <h2>Title Two</h2>
                <div class="content">
     @*               @{string text = Model.mytypeitem.MyType.MyField.Value;}  *@
                    <p class="text-field"> <span class="value">@text</span></p>
     @* et cetera *@
Nov 24, 2013 at 8:28 PM
well the blackout ..
changing 'dynamic MyTypeItem = Model.MyTypeItem' to:
dynamic MyTypeItem = Model.ContentItem;
string text = MyTypeItem.MyType.MyField.Value;
....... to solved the issue!
thanks for anybody spending time