Content Part Issue

Topics: Administration, Core, Customizing Orchard, General, Writing modules
Dec 17, 2012 at 1:32 PM
Edited Dec 17, 2012 at 1:33 PM

Hi,

I have Created costume content part that was successfully created, it is displaying in Features ad admin panel i was enabled that, my problem is the content part not showing in a list while i am going to add that into the page as my created content part is a part of that page, any suggestion regarding this issue would be help full.

Thanks in advance

Developer
Dec 17, 2012 at 1:39 PM
Edited Dec 17, 2012 at 1:39 PM

When you go to the Content Parts list, does your content part appear there? If so, make sure that its Attachable property is set to true so you can attach it to content types via the dashboard. Also make sure that your content part's name ends with "Part" (e.g. MyCustomPart). Otherwise it won't show up in the Content Parts list.

Dec 17, 2012 at 2:16 PM

Hi, 

i did all those things already but since it does not displaying, i have followed "http://docs.orchardproject.net/Documentation/Writing-a-content-part" this link to create content part the map part they have given as an example that was displaying in the list, but my content part name as FileUpload for the module and UploadPart that is a  part name it does not displaying.

Developer
Dec 17, 2012 at 4:26 PM
Edited Dec 17, 2012 at 4:27 PM

So when you follow the tutorial, you see the content part in the list, but when you follow the exact same steps with "UploadPart" you don't see that one appear in the list. That is very strange. It should work, and it's hard to tell what could be wrong without seeing any code. My advice is to try again from scratch, perhaps even trying a different name for the part (e.g. MyTestPart) as a sanity check.

Dec 18, 2012 at 6:06 AM

Hi,

i just fix that problem, you right sfmskywalker i did something wrong, now i just recreated the content part again, now its shows in list i added that into part but now i face another problem, the problem is while i added the content part into the page type after i go for specific page edit option i seen the following error, 

The model item passed into the dictionary is of type 'FileUploads.Models.FileUploadPart', but this dictionary requires a model item of type 'Orchard.Contrib.FileUpload.Models.FileUpoadViewModel'.

i dont know how to create viewmodels for this, if any suggestion for this it would be more helpfull.


Developer
Dec 18, 2012 at 9:51 AM

You created your view template to accept a model of type FileUploadViewModel, so you will have to pass in an instance of that type instead of the FileUploadPart instance. To see how that works, have a look at BlogArchivesPartDriver in the Blogs module.

Dec 18, 2012 at 1:20 PM

i created viewmodel as u told, but now the content part doesn't display in page edit but it showing Edit Placement - Page

Developer
Dec 18, 2012 at 3:12 PM

Not sure what you mean by that, but usually if a part shape doesn't render it is caused by an exception. To find out, check the log files and or attach the debugger (and perhaps even enable Debugging Assistants to break on handled exceptions as well). It's difficult to say what you did wrong without seeing code.

Dec 18, 2012 at 4:16 PM

follwings are my content part source:

FileUpload/Models:

FileUploadRecord.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Records;
namespace FileUploads.Models
{
public class FileUploadRecord : ContentPartRecord {
public virtual string Name { get; set; }
public virtual string Email { get; set; }
}
}
FileUploadPart.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement;
using System.ComponentModel.DataAnnotations;
namespace FileUploads.Models
{   
public class FileUploadPart : ContentPart<FileUploadRecord>   
{        [Required]       
public string Name       
{           
get { return Record.Name; }           
set { Record.Name = value; }       
}
        [Required]       
public string Email       
{            get { return Record.Email; }
           
set { Record.Email = value; }        }   
}
}
FileUpload/Handlers:
FileUploadHandler.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using FileUploads.Models;

using Orchard.ContentManagement.Handlers;

using Orchard.Data;

namespace FileUploads.Handlers

{

    public class FileUploadHandler : ContentHandler

    {

        public FileUploadHandler(IRepository<FileUploadRecord> repository)

        {

            Filters.Add(StorageFilter.For(repository));

        }

    }

}

 

FileUpload/Drivers:

FileUploadDriver.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using FileUploads.Models;

using Orchard.ContentManagement;

using Orchard.ContentManagement.Drivers;

using FileUploads.ViewModels;

 

namespace FileUploads.Drivers

{

    public class FileUploadDriver : ContentPartDriver<FileUploadPart> 

    {

        protected override string Prefix

        {

            get { return "FileUpload"; }

        }

        protected override DriverResult Display(FileUploadPart part, string displayType, dynamic shapeHelper)

        {

            return ContentShape("Parts_FileUpload", () => shapeHelper.Parts_FileUpload(

                Name: part.Name,

                Email: part.Email));

        }

 

       //GET

        protected override DriverResult Editor(FileUploadPart part, dynamic shapeHelper)

        {

            var viewModel = new FileUploadViewModel

            {

                Name=part.Name,

                Email=part.Email

            };

            return ContentShape("Parts_FileUpload_Edit", () => shapeHelper.EditorTemplate(

                TemplateName: "Parts/FileUpload",

                    Model: viewModel,

                    Prefix: Prefix));

        }

 

        //POST

        protected override DriverResult Editor(FileUploadPart part, Orchard.ContentManagement.IUpdateModel updater, dynamic shapeHelper)

        {

            var viewModel = new FileUploadViewModel();

            if (updater.TryUpdateModel(viewModel, Prefix, null, null))

            {

                part.Name=viewModel.Name;

                part.Email = viewModel.Email;

            }

            //return Editor(part, shapeHelper);    

            return ContentShape("Parts_FileUpload_Edit", () => shapeHelper.EditorTemplate(

                TemplateName: "Parts/FileUpload",

                    Model: viewModel,

                    Prefix: Prefix));

        }

    }

}

 

Views/EditorTemplates/Parts:

FileUpload.cshtml

@model FileUploads.ViewModels.FileUploadViewModel

@using FileUploads.Models

@using Orchard.ContentManagement;

<fieldset>

<legend>

File Upload Fields

</legend>

@Html.LabelFor(model=>model.Name)

@Html.EditorFor(model=>model.Name)

@Html.ValidationMessageFor(model=>model.Name)

@Html.LabelFor(model=>model.Email)

@Html.EditorFor(model=>model.Email)

@Html.ValidationMessageFor(model=>model.Email)

</fieldset>

Views/Parts:

FileUpload.cshtml

@model FileUploads.ViewModels.FileUploadViewModel

@using FileUploads.Models

@using Orchard.ContentManagement;

<fieldset>

<legend>

File Upload Fields

</legend>

@Html.LabelFor(model => model.Name)

@Html.TextBoxFor(model => model.Name)

@Html.ValidationMessageFor(model => model.Name)

@Html.LabelFor(model => model.Email)

@Html.TextBoxFor(model => model.Email)

@Html.ValidationMessageFor(model=>model.Email)

</fieldset>

Placement.info

<Placement>

  <Place Parts_FileUpload="Content:10"/>

   <Place Parts_FileUpload_Edit="Content:7.5"/>

</Placement>

 

Migration.cs

using System;

using System.Collections.Generic;

using System.Data;

using Orchard.ContentManagement.Drivers;

using Orchard.ContentManagement.MetaData;

using Orchard.ContentManagement.MetaData.Builders;

using Orchard.Core.Contents.Extensions;

using Orchard.Data.Migration;

using FileUploads.Models;

using Orchard.ContentManagement.Drivers;

using Orchard.ContentManagement.MetaData;

using Orchard.ContentManagement.MetaData.Builders;

using Orchard.Core.Contents.Extensions;

using Orchard.Data.Migration;

 

namespace FileUploads {

    public class Migrations : DataMigrationImpl {

        public int Create() {

// Creating table CX_FileUploads_FileUploadRecord

SchemaBuilder.CreateTable("CX_FileUploads_FileUploadRecord", table => table

.ContentPartRecord()

.Column("Name", DbType.String)

.Column("Email", DbType.String)

);

            ContentDefinitionManager.AlterPartDefinition(

                typeof(FileUploadPart).Name, cfg => cfg.Attachable());

            return 1;

        }

    }

}

 

The aboves are my source for my content part, i just did as above, but i cant get the content part it in edit page 

Developer
Dec 18, 2012 at 4:25 PM

Did you check the log files?

Dec 18, 2012 at 4:26 PM

Where i check that file, can u please guide me because i am new in orchard.

Developer
Dec 18, 2012 at 4:48 PM

Sure. Stuff that happens in Orchard gets logged in text files, which are stored in App_Data/Logs. There's a debug version and an error version. The Debug version contains both debug information as well as errors, so I usually open that one. Look at the last entries to see if there's anything suspicious going on.

Dec 18, 2012 at 4:55 PM

yuh.. you are right i just debug the solution i got a following error,

Generic ADOException was caught

could not load an entity: [FileUploads.Models.FileUploadRecord#61][SQL: SELECT fileupload0_.Id as Id489_0_, fileupload0_.Name as Name489_0_, fileupload0_.Email as Email489_0_ FROM CX_FileUploads_FileUploadRecord fileupload0_ WHERE fileupload0_.Id=?]

 

whats it mean...

Developer
Dec 18, 2012 at 5:00 PM
Edited Dec 18, 2012 at 5:00 PM

I think the problem is that NHibernate can't find a table mapping for your entity, and has most likely to do with the following name you used in Migrations: "CX_FileUploads_FileUploadRecord".

Change that into "FileUploadRecord", run your migrations again (deleting everything from App_Data is the easiest way, if you don't mind starting over), and you should be fine.