Trying to understand how best to handle populating data for a ContentPart

Topics: Writing modules
Feb 20, 2015 at 5:56 PM
I wasn't completely sure how best to "describe" my problem in the subject, so I'm hopeful someone will understand my question from the details below. A little background, I'm migrating an application from stock ASP.NET MVC 3 to Orchard.

I have a ContentPart in my custom module called ClientProfilePart. This part has a property (or do you call it field?) named "County". It's simply of type string. I'd like to display a dropdownlist for this particular field (got that figured out thanks to IDeliverable blogs). What I'm confused about is where/how to store the County data.

Since county is just a simple one field string, it seems overkill to create a CountyPart to query. That being said, I'd also like to allow the administrator to add/remove counties from the system. What is the best practice here?

To summarize: What is the best way to handle managed (that is administrators can add/remove items) drop down lists for simple types like County?

The end result? The ClientProfilePart was added to the User Content Type. When a user registers on the site, they fill out a Client Profile and must select the county they live in as part of their profile.
Feb 20, 2015 at 6:18 PM
Does it make sense that I would create a County Content Type with a simple Text Field as part of my Migrations in the module? Seems to me that would give my admins the ability to create counties and my module would be able to query the counties as part of my Drivers for displaying the ClientProfile. Problem is, how do I create a ContentType in the Migrations? I understand how to alter a ContentType (did that to add the ClientProfile to the User ContentType).

Thanks!
Paul
Feb 20, 2015 at 6:33 PM
Thanks to the DOJO tutorials, I now realize that "AlterTypeDefinition" will also create the ContentType if it does not already exist. I'm still curious as to whether the way I'm going about this is a common practice.

Summary of how I'm going about creating a client profile with a county drop down list that is manageable by admins.
  1. Create Content Part called ClientProfilePart with FirstName, LastName, MiddleInitial, and County
  2. Create a Content Type named County with a TitlePart
  3. Create a ClientProfileViewModel with a SelectList for counties
  4. Query the County content types in the Driver and assign the list of counties to the Counties select list.
  5. Create the view that will display the ClientProfileViewModel.
Developer
Feb 21, 2015 at 9:40 AM
Good to see you figured it out.
I'd say this is a pretty common practice. I did something similar for OrchardPros where a user is able to specify their country and that I defined the Country entity as a POCO, not as a content type. The reason I didn't define it as a content type is that I needed none of the features one would gain from being a content type.
Feb 22, 2015 at 12:06 AM
Thanks sfmskywalker,

I certainly learned a LOT about Orchard in the last couple days compared to the last few years using it purely as a CMS (no coding at all). At first, I didn't think there was much documentation out there, but the more I dug, the more I realized there is plenty. You have to be willing to take leaps, mess things up a bit, and start over to really grasp everything. I'm feeling much more confident now. The IDeliverable blogs are very helpful.

http://www.ideliverable.com/blog
Feb 23, 2015 at 9:17 AM
Why wouldn't you just add an enumeration field to the content type or content part? This could even be done through the dashboard.

Also migrations would work:
ContentDefinitionManager.AlterPartDefinition(typeof(ClientProfilePart).Name,

    part => part
        .WithField("County",
            field => field
                .OfType("EnumerationField")
                .WithSetting("EnumerationFieldSettings.Options",
                    string.Join(System.Environment.NewLine,
                        new[] {"foo", "bar", "baz"})); // defaults, can be changed in the dashboard
);
Feb 23, 2015 at 3:43 PM
Perfect! Thank you sanderg. A much simpler solution and it meets all my requirements.