Orchard CMS ContentManager.New<>() Specified Cast Was Invalid

Topics: Writing modules
Oct 26, 2013 at 11:27 PM
I am in the early stages of developing a new module.

Much of it is laid out in terms of the models etc. Also have the migrations all set up and my database now has the tables for my module.

I am encountering the following error when calling ContentManager.New<myPart> and would like some help please.

Error is this:
An unhandled exception has occurred and the request was terminated. Please refresh the page. If the error persists, go back
Specified cast is not valid.
System.InvalidCastException: Specified cast is not valid. 
at Orchard.ContentManagement.ContentCreateExtensions.New[T]
              (IContentManager manager, String contentType) 


The chunk of code that fires the exception is this:
public static T New<T>(this IContentManager manager, string contentType) where T : class, IContent {
            var contentItem = manager.New(contentType);
            if (contentItem == null)
                return null;

            var part = contentItem.Get<T>();
            if (part == null)
                throw new InvalidCastException();

            return part;
        }


Here are the various parts to my module that are related to the operation i am struggling with:


ContentPart
public class GoogleMapsSettingsPart : ContentPart<GoogleMapsSettingsPartRecord>
{
    public string ApiKey { 
        get { return Record.ApiKey; }
        set { Record.ApiKey = value; }
    }
}
ContentPartRecord
public class GoogleMapsSettingsPartRecord : ContentPartRecord
{
    public virtual string ApiKey { get; set; }
}

Handler
public GoogleMapsSettingsPartHandler(IRepository<GoogleMapsSettingsPartRecord> repository)
        {
            Filters.Add(StorageFilter.For(repository));
        }

Migration for this table
// Settings Table
SchemaBuilder.CreateTable("GoogleMapsSettingsPartRecord", table => table
       .ContentPartRecord()
       .Column("ApiKey", DbType.String, c => c.WithLength(60))
);

Some of the code from the controller for this model etc
public AdminController(IContentManager contentManager, IShapeFactory shapeFactory, IServiceLocatorService serviceLocatorService, INotifier notifier)
        {
            _contentManager = contentManager;
            _serviceLocatorService = serviceLocatorService;
            _notifier = notifier;
            Shape = shapeFactory;
            T = NullLocalizer.Instance;
        }


        /// <summary>
        /// Display Settings
        /// </summary>
        /// <returns></returns>
        public ActionResult Settings()
        {
            var settings = _serviceLocatorService.GoogleMapsSettings;
            var editor = CreateSettingsEditor(settings);
            var model = _services.ContentManager.BuildEditor(settings);

            return View((object)model);
        }

Finally - the Services where my call throws this exception
private GoogleMapsSettingsPart _settings;
        public GoogleMapsSettingsPart GoogleMapsSettings
        {
            get {
                if (_settings == null)
                {
                    _settings = _contentManager.Query<GoogleMapsSettingsPart, GoogleMapsSettingsPartRecord>().List().FirstOrDefault();

                    if (_settings == null)
                    {
                        _settings = _contentManager.New<GoogleMapsSettingsPart>("GoogleMapsSettings");
                    }
                }
                return _settings;
            }
        }

The actual line where the exception happens is _settings = _contentManager.New<GoogleMapsSettingsPart>("GoogleMapsSettings");

I have tried all sorts of stuff in place of "GoogleMapsSettings" though nothing is working.

I'm pretty sure at this point it's something simple, though it's avoiding me..My limited knowledge of Orchard is stumping me

Any help would be appreciated :)
Oct 27, 2013 at 9:24 PM

Have you created the content type itself? You need a content type with your part attached. Look for AlterTypeDefinition calls in the code.

27 Eki 2013 18:54 tarihinde "darrenthebrit" <[email removed]> yazdı:

From: darrenthebrit

I am in the early stages of developing a new module.

Much of it is laid out in terms of the models etc. Also have the migrations all set up and my database now has the tables for my module.

I am encountering the following error when calling ContentManager.New<myPart> and would like some help please.

Error is this:
An unhandled exception has occurred and the request was terminated. Please refresh the page. If the error persists, go back
Specified cast is not valid.
System.InvalidCastException: Specified cast is not valid. 
at Orchard.ContentManagement.ContentCreateExtensions.New[T]
              (IContentManager manager, String contentType) 


The chunk of code that fires the exception is this:
public static T New<T>(this IContentManager manager, string contentType) where T : class, IContent {
            var contentItem = manager.New(contentType);
            if (contentItem == null)
                return null;

            var part = contentItem.Get<T>();
            if (part == null)
                throw new InvalidCastException();

            return part;
        }


Here are the various parts to my module that are related to the operation i am struggling with:


ContentPart
public class GoogleMapsSettingsPart : ContentPart<GoogleMapsSettingsPartRecord>
{
    public string ApiKey { 
        get { return Record.ApiKey; }
        set { Record.ApiKey = value; }
    }
}
ContentPartRecord
public class GoogleMapsSettingsPartRecord : ContentPartRecord
{
    public virtual string ApiKey { get; set; }
}

Handler
public GoogleMapsSettingsPartHandler(IRepository<GoogleMapsSettingsPartRecord> repository)
        {
            Filters.Add(StorageFilter.For(repository));
        }

Migration for this table
// Settings Table
SchemaBuilder.CreateTable("GoogleMapsSettingsPartRecord", table => table
       .ContentPartRecord()
       .Column("ApiKey", DbType.String, c => c.WithLength(60))
);

Some of the code from the controller for this model etc
public AdminController(IContentManager contentManager, IShapeFactory shapeFactory, IServiceLocatorService serviceLocatorService, INotifier notifier)
        {
            _contentManager = contentManager;
            _serviceLocatorService = serviceLocatorService;
            _notifier = notifier;
            Shape = shapeFactory;
            T = NullLocalizer.Instance;
        }


        /// <summary>
        /// Display Settings
        /// </summary>
        /// <returns></returns>
        public ActionResult Settings()
        {
            var settings = _serviceLocatorService.GoogleMapsSettings;
            var editor = CreateSettingsEditor(settings);
            var model = _services.ContentManager.BuildEditor(settings);

            return View((object)model);
        }

Finally - the Services where my call throws this exception
private GoogleMapsSettingsPart _settings;
        public GoogleMapsSettingsPart GoogleMapsSettings
        {
            get {
                if (_settings == null)
                {
                    _settings = _contentManager.Query<GoogleMapsSettingsPart, GoogleMapsSettingsPartRecord>().List().FirstOrDefault();

                    if (_settings == null)
                    {
                        _settings = _contentManager.New<GoogleMapsSettingsPart>("GoogleMapsSettings");
                    }
                }
                return _settings;
            }
        }

The actual line where the exception happens is _settings = _contentManager.New<GoogleMapsSettingsPart>("GoogleMapsSettings");

I have tried all sorts of stuff in place of "GoogleMapsSettings" though nothing is working.

I'm pretty sure at this point it's something simple, though it's avoiding me..My limited knowledge of Orchard is stumping me

Any help would be appreciated :)

Read the full discussion online.

To add a post to this discussion, reply to this email ([email removed])

To start a new discussion for this project, email [email removed]

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Oct 27, 2013 at 10:32 PM
Hi,

thanks for your reply.

I had created it. Early on I had the following in my migrations
ContentDefinitionManager.AlterTypeDefinition("GoogleMapsSettings", builder => builder.Build());
nothing i did seemed to work.

I have, since, got it working a little differently though. In my case i didn't really want a contentType visible in the admin for this part. so amended my handler with the following code:
Filters.Add(new ActivatingFilter<GoogleMapsSettingsPart>("GoogleMapsSettings"));
Filters.Add(StorageFilter.For(repository));
Filters.Add(new TemplateFilterForRecord<GoogleMapsSettingsPartRecord>("GoogleMapsSettings", "Parts/GoogleMapsSettings"));
From what i gather this creates the type temporarily for the purpose of the rest of the execution.

i would be really interested to know what i was doing wrong in my original question though...?...
Coordinator
Oct 28, 2013 at 5:36 AM
You created the type but you did not attach the part to it.