unable to save the custom content part.

Topics: Writing modules
May 9, 2012 at 8:10 AM

Hi, I am creating a new Orchard Module for events. Here is the part and record class

public class SupportedEmploymentEventPartRecord : ContentPartRecord
    {
        public virtual String EventTitle { get; set; }
        public virtual String Description { get; set; }
        public virtual DateTime StartDate { get; set; }
        public virtual DateTime? EndDate { get; set; }
        public virtual String Location { get; set; }
        public virtual String CityName { get; set; }
        public virtual String State { get; set; }
        public virtual String PostalCode { get; set; }
        public virtual String ContactName { get; set; }
        public virtual String ContactEmail { get; set; }
        public virtual String ContactPhoneNumber { get; set; }
        public virtual DateTime CreateDate { get; set; }
        public virtual DateTime? LastModifiedDate { get; set; }
    }

    public class SupportedEmploymentEventPart : ContentPart<SupportedEmploymentEventPartRecord>
    {
        [Required]
        public String EventTitle
        {
            get { return Record.EventTitle; }
            set { Record.EventTitle = value; }
        }

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

        [Required]
        public DateTime StartDate
        {
            get { return Record.StartDate; }
            set { Record.StartDate = value; }
        }
               
        public DateTime? EndDate
        {
            get { return Record.EndDate; }
            set { Record.EndDate = value; }
        }

        public String Location
        {
            get { return Record.Location; }
            set { Record.Location = value; }
        }
        public String CityName
        {
            get { return Record.CityName; }
            set { Record.CityName = value; }
        }

        public String State
        {
            get { return Record.State; }
            set { Record.State = value; }
        }

        public String PostalCode
        {
            get { return Record.PostalCode; }
            set { Record.PostalCode = value; }
        }

        public String ContactName
        {
            get { return Record.ContactName; }
            set { Record.ContactName = value; }
        }

        public String ContactEmail
        {
            get { return Record.ContactEmail; }
            set { Record.ContactEmail = value; }
        }

        public String ContactPhoneNumber
        {
            get { return Record.ContactPhoneNumber; }
            set { Record.ContactPhoneNumber = value; }
        }

        public DateTime CreateDate
        {
            get { return Record.CreateDate; }
            set { Record.CreateDate = value; }
        }

        public DateTime? LastModifiedDate
        {
            get { return Record.LastModifiedDate; }
            set { Record.LastModifiedDate = value; }
        }

    }
Here is the create method in the migration class
public int Create()
        {   

            this.SchemaBuilder.CreateTable(typeof(SupportedEmploymentEventPartRecord).Name
                , table => table
                    .ContentPartRecord()
                    .Column<String>("EventTitle", c => c.NotNull().WithLength(1024))//public virtual String EventTitle { get; set; }
                    .Column<String>("Description", c => c.Unlimited())//public virtual String Description { get; set; }
                    .Column<DateTime>("StartDate", c => c.NotNull())//public virtual DateTime StartDate { get; set; }
                    .Column<DateTime>("EndDate", c => c.Nullable())//public virtual DateTime? EndDate { get; set; }
                    .Column<String>("Location", c => c.Unlimited().Nullable())//public virtual String Location { get; set; }
                    .Column<String>("State", c => c.WithLength(255).Nullable())//public virtual String State { get; set; }
                    .Column<String>("CityName", c => c.WithLength(244).Nullable())//public virtual String CityName { get; set; }
                    .Column<String>("PostalCode", c => c.WithLength(20).Nullable())//public virtual String PostalCode { get; set; }
                    .Column<String>("ContactName", c => c.WithLength(1024).Nullable())//public virtual String ContactName { get; set; }
                    .Column<String>("ContactEmail", c => c.WithLength(255).Nullable())//public virtual String ContactEmail { get; set; }
                    .Column<String>("ContactPhoneNumber", c => c.WithLength(20).Nullable())//public virtual String ContactPhoneNumber { get; set; }
                    .Column<DateTime>("CreateDate", c => c.NotNull())//public virtual DateTime CreateDate { get; set; }
                    .Column<DateTime>("LastModifiedDate", c => c.Nullable())//public virtual DateTime? LastModifiedDate { get; set; }
            );

            this.ContentDefinitionManager.AlterTypeDefinition("SupportedEmploymentEvent", c =>
                c.WithPart(typeof(CommonPart).Name)
                .WithPart(typeof(SupportedEmploymentEventPart).Name)
                );
                

            return 1;
        }

I also have created handler and driver.

Everything looks good and table is created in the orchard database with the defined schema.

According to documentation I only had to write following code to save the data in the table.

var supportedEvent = Services.ContentManager.New<SupportedEmploymentEventPart>(Constant.SupportedEmploymentEvent);

            this.Services.ContentManager.Create(supportedEvent);
            var editor = this.Services.ContentManager.UpdateEditor(supportedEvent, this);
            if (this.ModelState.IsValid == false)
            {
                Services.TransactionManager.Cancel();
                return View((object)editor);
            }               

            return RedirectToAction("Index");

(To be fair, I am unable to understand how this code is working)

But this code is not working for me. It is not saving the data in the table. I tried to figure out the problem and found that my part is not updated with the posted data so I just inserted a line between New and Create call. line of code is

TryUpdateModel(supportedEvent);

Now it started saving data.

I am unable to figure out where I am doing wrong. Can anybody please help me.

 

thanx

 

Coordinator
May 9, 2012 at 5:27 PM

What documentation topic is that? You should never have to create a part from a driver.