Basic Custom Part mistakes

Topics: Writing modules
Apr 2, 2013 at 7:21 PM
Hi there, I'm trying to write a module, which will have some custom parts/types defined, and I'm struggling to get just the basic CRUD operations working.

So far I have defined the type and part, and the migrations all seem to work. The Driver and shape are all displaying when I click to create new, but when I hit save, it is not getting to the database. The driver is so bone-head simple, I can't see what I am possibly doing wrong, but I know it must be something simple. I've actually gotten this working before, but I can't seem to duplicate my previous efforts (It's been months since I had a chance to actually write Orchard code.
Here's some code (I left out usings/namespace, etc, for brevity). Note the comment in the Driver, which describes the symptom best:
//Models/CompetencyPart.cs:
    public class CompetencyPart : ContentPart<CompetencyPartRecord>
    {
        public string Name {
            get { return Record.Name; }
            set { value = Record.Name; }
        }

        public string Description {
            get { return Record.Description; }
            set { value = Record.Description; }
        }
    }

// Models/CompetencyPartRecord.cs:
    public class CompetencyPartRecord : ContentPartRecord
    {
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }
    }

//Drivers/CompetencyPartDriver.cs:
    public class CompetencyPartDriver : ContentPartDriver<CompetencyPart>
    {
        protected override string Prefix {
            get { return "Competency"; }
        }

        protected override DriverResult Editor(CompetencyPart part, dynamic shapeHelper) {
            return ContentShape("Parts_Competency_Edit", () => shapeHelper.EditorTemplate(TemplateName: "Parts/Competency", Model: part, Prefix: Prefix));
        }

        protected override DriverResult Editor(CompetencyPart part, IUpdateModel updater, dynamic shapeHelper) {
            updater.TryUpdateModel(part, Prefix, null, null); // part.Name & part.Description are both null, so the record that gets saved has nothing.
            return Editor(part, shapeHelper);
        }
    }
//Handlers/CompetencyPartHandler.cs:
    public class CompetencyPartHandler : ContentHandler
    {
        public CompetencyPartHandler(IRepository<CompetencyPartRecord> repository)
        {
            Filters.Add(StorageFilter.For(repository));
        }
    }

//Views/EditorTemplates/Parts/Competency.cshtml:
@model com.MedTrack.Competencies.Models.CompetencyPart
@using System.Web.Mvc.Html
@using com.MedTrack.Competencies.Models
@{
    CompetencyPart part = (CompetencyPart) Model;
}
<fieldset>
    <legend>Competency Fields</legend>
 
    <div class="editor-label">@Html.LabelFor(x => x.Name)</div>
    <div class="editor-field">
        @Html.EditorFor(x => x.Name)
        @Html.ValidationMessageFor(x => x.Name)
    </div>

     <div class="editor-label">@Html.LabelFor(x => x.Description)</div>
    <div class="editor-field">
        @Html.EditorFor(x => x.Description)
        @Html.ValidationMessageFor(x => x.Description)
    </div>
</fieldset>

//Migrations.cs:
    public class Migrations : DataMigrationImpl {
        private readonly ITaxonomyService _taxService;

        public Migrations(ITaxonomyService taxonomyService) {
            _taxService = taxonomyService;

        }

        public int Create() {
            SchemaBuilder.CreateTable("CompetencyPartRecord", table => table
                    .ContentPartRecord()
                    .Column<string>("Name")
                    .Column<string>("Description")
                );

            ContentDefinitionManager.AlterPartDefinition("CompetencyPart", part => part.Attachable());

            ContentDefinitionManager.AlterTypeDefinition("Competency", cfg => cfg
                .WithPart("CompetencyPart")
                .Creatable()
            );

            return 1;
        }
    }
Coordinator
Apr 5, 2013 at 8:33 AM
It's Record.Whatever = value, not the other way around.
Apr 5, 2013 at 6:11 PM
Wow. I knew it would be something simple. I didn't realize it would be stupid too. =)
Thanks Bertrand.

And apologies to you and anyone that lost time staring at it, trying to help!