Custom fields

Topics: Writing modules
Jun 3, 2012 at 7:41 AM

After completing the writing a module with a text editor tutorial, I wrote another module which is based on the tutorial.

When I open Orchard I see my new module but all the fields for the new model I defined for this module are not showing, although the DB tables were created.

What can be the issue here?   where should I look first in order to find what exactly went wrong?

Developer
Jun 3, 2012 at 11:04 AM

(I guess by "fields" you actually mean properties of a content part here, correct? Because fields have another meaning in Orchard.) Make sure you correctly set up your templates, your drivers return the correct shapes using the templates and shapes have a placement defined. Otherwise we could help better if you'd explain what you've done so far and show some code too.

Jun 3, 2012 at 11:25 AM

Hi

this is what I have done so far:

the Model:

public class CustomerPartRecord : ContentPartRecord
    {
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual int PhoneNumber { get; set; }
        public virtual string Address { get; set; }
        public virtual string Profession { get; set; }
        public virtual string ProDescription { get; set; }
        public virtual int Hours { get; set; }
    }
    public class CustomerPart : ContentPart<CustomerPartRecord>
    {

        [Required(ErrorMessage="you must enter your first name")]
        [StringLength(200)]
        public string FirstName { get { return Record.FirstName; } set { Record.FirstName = value; } }

        [Required(ErrorMessage = "you must enter your last name")]
        [StringLength(200)]
        public string LastName { get { return Record.LastName; } set { Record.LastName = value; } }

        [Required(ErrorMessage = "you must enter your phone number")]
        [DataType(DataType.PhoneNumber)]
        public int PhoneNumber { get { return Record.PhoneNumber; } set { Record.PhoneNumber = value; } }

        [StringLength(200)]
        public string Address { get { return Record.Address; } set { Record.Address = value; } }

        [Required(ErrorMessage = "you must enter your profession")]
        [StringLength(200)]
        public string Profession { get { return Record.Profession; } set { Record.Profession = value; } }

        [StringLength(500)]
        public string ProDescription { get { return Record.ProDescription; } set { Record.ProDescription = value; } }

        [Required(ErrorMessage = "you must enter your hours")]
        public int Hours { get { return Record.Hours; } set { Record.Hours = value; } }
    }

 

the driver:

 

class CustomerDriver : ContentPartDriver<CustomerPart>
    {
        protected override DriverResult Display(CustomerPart part, string displayType, dynamic shapeHelper)
        {
            return ContentShape("Parts_Customer", () => shapeHelper.Parts_BankCustomer(
                FirstName: part.FirstName,
                LastName: part.LastName,
                PhoneNumber: part.PhoneNumber,
                Address: part.Address,
                Profession: part.Profession,
                ProDescription: part.ProDescription,
                Hours: part.Hours));
        }
        //GET
        protected override DriverResult Editor(CustomerPart part, dynamic shapeHelper)
        {
            return ContentShape("Parts_Customer", () => shapeHelper.EditorTemplate(
                TemplateName:"Parts/Customer",
                Model: part,
                Prefix: Prefix));
        }
        //POST
        protected override DriverResult Editor(CustomerPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            updater.TryUpdateModel(part, Prefix, null, null);
            return Editor(part, shapeHelper);
        }
    }

 

the migration:

 

public class Migrations : DataMigrationImpl
    {
        public int Create()
        {
            // Creating table CustomerPartRecord
            SchemaBuilder.CreateTable("CustomerPartRecord", table => table
                .ContentPartRecord()
                .Column("FirstName", DbType.String)
                .Column("LastName", DbType.String)
                .Column("PhoneNumber", DbType.Int32)
                .Column("Address", DbType.String)
                .Column("Profession", DbType.String)
                .Column("ProDescription", DbType.String)
                .Column("Hours", DbType.Int32)
            );
            return 1;
        }
        public int UpdateFrom1()
        {
            ContentDefinitionManager.AlterPartDefinition("CustomerPart",
              builder => builder.Attachable());
            return 2;
        }
        public int UpdateFrom2()
        {
            ContentDefinitionManager.AlterTypeDefinition("Customer", cfg => cfg
              .WithPart("CommonPart")
              .WithPart("RoutePart")
              .WithPart("BodyPart")
              .WithPart("CustomerPart")
              .WithPart("CommentsPart")
              .WithPart("TagsPart")
              .WithPart("LocalizationPart")
              .Creatable()
              .Indexed());
            return 3;
        }

    }

 

the view:

@model TimeProject.Models.CustomerPart
<fieldset>
<legend>Customer</legend>
    <label class="sub" for="FirstName">First Name</label><br />
    @Html.TextBoxFor(m => m.FirstName, new { @class = "text" })<br />
    <label class="sub" for="LastName">Last Name</label><br />
    @Html.TextBoxFor(m => m.LastName, new { @class = "text" })
</fieldset>
I have included the EditorTemplates folder and the parts folder. I did this tutorial with the original names and everything went smooth, I can't find the reason why this doesn't work.

Developer
Jun 3, 2012 at 2:05 PM

You forgot the placement.info for your Display and Editor shapes.

Something like :

<Placement>

<Place Parts_Customer="Content:5"/>

<Place Parts_Customer_Edit="Content:5"/>

</Placement>

Jun 3, 2012 at 2:24 PM

I did add the file to the module directory. but instead of "Content:5" I put "Content:3" does this matter? I also placed a the proper references in the csproj file 

<ItemGroup>
    <Content Include="Web.config" />
    <Content Include="Views\Web.config" />
    <Content Include="Scripts\Web.config" />
    <Content Include="Styles\Web.config" />
    <Content Include="Properties\AssemblyInfo.cs" />
    <Content Include="placement.info" />
    <Content Include="Views\Parts\Customer.cshtml" />
    <Content Include="Views\EditorTemplates\Parts\Customer.cshtml" />
    <Content Include="Module.txt" />
  </ItemGroup>
<ItemGroup>
    <Compile Include="Drivers\CustomerDriver.cs" />
    <Compile Include="Handlers\CustomerHandler.cs" />
    <Compile Include="Migrations.cs" />
    <Compile Include="Models\Customer.cs" />
  </ItemGroup>
where else should I look? this doesn't make any sense, it supposed to work :)

Developer
Jun 3, 2012 at 3:18 PM
Edited Jun 3, 2012 at 3:19 PM
In Driver,  Display : 
shapeHelper.Parts_BankCustomer
Should be:
shapeHelper.Parts_Customer

In Editor :
return ContentShape("Parts_Customer", () => shapeHelper.EditorTemplate(
Should be :
return ContentShape("Parts_Customer_Edit", () => shapeHelper.EditorTemplate(
Jun 3, 2012 at 5:35 PM

thank you, I fixed that and rebuild the solution, but I can't see my Properties yet. do I need to create the module again?

Coordinator
Jun 4, 2012 at 4:56 PM

Anything suspicious in app_data\logs?

Jun 4, 2012 at 6:07 PM

can you please elaborate? where should I look because I don't get any warning or errors during the build process or in debug mode. 

Developer
Jun 4, 2012 at 6:42 PM

If something doesn't show up while expected to, it is usually caused by some error happening at runtime. If the error is non-fatal, it is logged and swallowed. If you check the latest log file in the app_data\logs folder, you may find some clues on why the properties aren't showing.

Jun 4, 2012 at 6:51 PM

Thank you, I will check it and try to figure out what went wrong.
By the way I love your tutorial about the adding a module to orchard it helped me to understand the Come better

Developer
Jun 4, 2012 at 7:06 PM

Thanks :)