var cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
var countries = cultures.Select(cultureInfo => new RegionInfo(cultureInfo.LCID)).Select(regionInfo => new { regionInfo.DisplayName, regionInfo.TwoLetterISORegionName }).Distinct().OrderBy(s => s.DisplayName).ToList();
I figured it might be parsing the compiled code from AppCode, so i have tried adding the following code ExampleDotNetSource.cs in AppCode:
using System.Collections.Generic;
using nuPickers.Shared.DotNetDataSource;
using System.Globalization;
public class ExampleDotNetDataSource : IDotNetDataSource
{
IEnumerable<KeyValuePair<string, string>> IDotNetDataSource.GetEditorDataItems(int contextId)
{
// return a collection of key / labels for picker
var cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
var countries = cultures.Select(cultureInfo => new RegionInfo(cultureInfo.LCID)).Select(regionInfo => new { regionInfo.DisplayName, regionInfo.TwoLetterISORegionName }).Distinct().OrderBy(s => s.DisplayName).ToList();
return countries;
}
}
and its throwing exception :
'System.Array' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
Am I missing a namespace or the routine call is not proper?
using System.Collections.Generic;
using nuPickers.Shared.DotNetDataSource;
using System.Globalization;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
public class ExampleDotNetDataSource : IDotNetDataSource
{
IEnumerable<KeyValuePair<string, string>> IDotNetDataSource.GetEditorDataItems(int contextId)
{
// return a collection of key / labels for picker
var cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
var countries = cultures.Select(cultureInfo => new RegionInfo(cultureInfo.LCID)).Select(regionInfo => new { regionInfo.DisplayName, regionInfo.TwoLetterISORegionName }).Distinct().OrderBy(s => s.DisplayName).ToList();
return countries;
}
}
What is the right way to return the countries from the routine as above gives the exception;
Cannot implicitly convert type 'System.Collections.Generic.List
If I remove the .ToList(); at the end of countries
like
var countries = cultures.Select(cultureInfo => new RegionInfo(cultureInfo.LCID)).Select(regionInfo => new { regionInfo.DisplayName, regionInfo.TwoLetterISORegionName }).Distinct().OrderBy(s => s.DisplayName);
return countries;
it throws exception:
Compiler Error Message: CS0266: Cannot implicitly convert type 'System.Linq.IOrderedEnumerable
I see now what the problem is. The datasource expects the return type to be of
IEnumerable<KeyValuePair<string, string>>
So you need to convert the countries to this type. Adding this code should probably be fine :
var list = new List<KeyValuePair<string,string>>();
foreach (var country in countries)
{
list.Add(new KeyValuePair(country.TwoLetterISORegionName,country.DisplayName);
}
return list;
Its very interesting indeed. Cuba is not there I can see from the list (i dont know what Others are).
If you didnt point this out I would have assumed the country list is complete...
I'll quote what the person said in your blog post for anyone chasing the same;
[quote] from MrSmoofy on 14th June 2011
Says:
GetCulters does not return a complete list of countries. It returns cultures. You will find the country “Cuba” and others missing from your list. I have found no way for Windows or .NET to provide you the complete list that matches the ISO 3166-1. If you look at Windows Control Panel Region and Language and the Location Tab the drop down there shows a list of all countries however this data is not in the registry or provided by .NET
I have a SQL Script that creates a database table and inserts a list of ISO3166 countries with alpha2, alpha3 and numeric3 codes. It may possibly be a little out of date now though.
Also found this that offers it in CSV and JSON format which might be of use:
I have renamed the datatype created with NuPickers DotNet above as CultureSelector. Its still adequate for the countries selection for the use case (just like you mentioned in your blog Simon).
With the T-SQL script added the Countries table to the Umbraco DB.
Then added new DataType with uDynamic, added the sql query Select countrycode,countryname FROM countries
and checked the jquery option.
How are you setting this up? Where are you placing your CompaniesDotNetDataSource.cs ? What are you refering to as Assembley in nupicker setup? I'm runing Umbraco 7.15.3, nuPickers 1.7.1 and don´t have a App_Code folder.
Country Dropdown Data Type
Used the uComponents country picker dataype long time ago.
In version 7 (7.1.8), how can I create a data type (dropdown list) property for backend user to select a Country?
I have cheched the projects page and they are all pre-v7.
If someone can chip in what is the ideal/quick way to add country dropdown to be used in document-type, would be highly appreciated.
cheers
I think this can easily be build using nuPickers and creating a custom datasource for it
Nupickers package : https://our.umbraco.org/projects/backoffice-extensions/nupickers/ Dotnetdatasource docs : https://github.com/uComponents/nuPickers/wiki/Data-Source-DotNet
Code to get country list from system :
Dave
Hi Dave
Thanks for the reply.
That looks very elegant!
However the nuPickers project page states: "Requires: Umbraco 7.2.3 or later"
and im stuck on 7.1.8 and unable to upgrade due to certain reasons.
You need to get the 1.3.0 version of nuPickers. You can find it on the Archived Files tab. I have this working in 7.1.x sites without problems.
Dave
Thats great to hear!
I did install and tried creating a nuPickers.DotNetDropDownPicker
datatype but it doesnt seem to show anything under the Assembly (and related class) attributes.
Am I missing something?
I figured it might be parsing the compiled code from AppCode, so i have tried adding the following code ExampleDotNetSource.cs in AppCode:
and its throwing exception :
'System.Array' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
Am I missing a namespace or the routine call is not proper?
You probably need to add this using statements as well :
Many thanks, added the using statements.
I am getting there...
What is the right way to return the countries from the routine as above gives the exception;
for the
return coutries;
line
You need to remove the .ToList() on the end of the line where we select the countries.
dave
Hi again Dave
If I remove the .ToList(); at the end of countries
like
it throws exception:
Ah,
I see now what the problem is. The datasource expects the return type to be of
So you need to convert the countries to this type. Adding this code should probably be fine :
Dave
Many thanks Dave! Got it working, appreciated introducing nuPickers DotNet and help with the code.
cheers!
You should be aware that you don't get a complete list of countries using this method. I blogged about this method back in 2010 and someone commented drawing my attention to the fact the list was incomplete as it uses only cultures. See the comments on my post here http://prolificnotion.uk/2010/07/23/c-utility-method-to-populate-list-controls-with-all-countries-as-given-in-iso-3166-1/
Thanks for the heads up Simon.
Its very interesting indeed. Cuba is not there I can see from the list (i dont know what Others are). If you didnt point this out I would have assumed the country list is complete...
I'll quote what the person said in your blog post for anyone chasing the same;
GetCulters does not return a complete list of countries. It returns cultures. You will find the country “Cuba” and others missing from your list. I have found no way for Windows or .NET to provide you the complete list that matches the ISO 3166-1. If you look at Windows Control Panel Region and Language and the Location Tab the drop down there shows a list of all countries however this data is not in the registry or provided by .NETI have a SQL Script that creates a database table and inserts a list of ISO3166 countries with alpha2, alpha3 and numeric3 codes. It may possibly be a little out of date now though.
Also found this that offers it in CSV and JSON format which might be of use:
http://data.okfn.org/data/core/country-list
I have renamed the datatype created with NuPickers DotNet above as CultureSelector. Its still adequate for the countries selection for the use case (just like you mentioned in your blog Simon).
As alternative I have found the uDynamic package at https://our.umbraco.org/projects/backoffice-extensions/udynamic/
Then used the T-SQL script from http://blog.xsql.com/2012/10/t-sql-list-of-countries.html I would be interested to check the one you have too.
With the T-SQL script added the Countries table to the Umbraco DB. Then added new DataType with uDynamic, added the sql query Select countrycode,countryname FROM countries and checked the jquery option.
This gives a write-ahead like jquery dropdown.
Here you go:
https://gist.github.com/ProNotion/b97e04b4f2961866298e
Hi All.
How are you setting this up? Where are you placing your CompaniesDotNetDataSource.cs ? What are you refering to as Assembley in nupicker setup? I'm runing Umbraco 7.15.3, nuPickers 1.7.1 and don´t have a App_Code folder.
/Oliver
is working on a reply...