Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • keilo 568 posts 1023 karma points
    Jan 21, 2016 @ 06:09
    keilo
    0

    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

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jan 21, 2016 @ 08:42
    Dave Woestenborghs
    5

    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 :

    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();
    

    Dave

  • keilo 568 posts 1023 karma points
    Jan 21, 2016 @ 08:45
    keilo
    0

    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.

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jan 21, 2016 @ 08:48
    Dave Woestenborghs
    2

    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

  • keilo 568 posts 1023 karma points
    Jan 21, 2016 @ 08:52
    keilo
    0

    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?

    Screenshot

  • keilo 568 posts 1023 karma points
    Jan 21, 2016 @ 09:02
    keilo
    0

    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?

    sshot

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jan 21, 2016 @ 09:06
    Dave Woestenborghs
    1

    You probably need to add this using statements as well :

     using System.Collections.Generic;
        using System.Globalization;
        using System.Linq;
    
  • keilo 568 posts 1023 karma points
    Jan 21, 2016 @ 09:13
    keilo
    0

    Many thanks, added the using statements.

    I am getting there...

    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

    for the

    return coutries;

    line

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jan 21, 2016 @ 09:16
    Dave Woestenborghs
    1

    You need to remove the .ToList() on the end of the line where we select the countries.

    dave

  • keilo 568 posts 1023 karma points
    Jan 21, 2016 @ 09:19
    keilo
    0

    Hi again Dave

    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
  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Jan 21, 2016 @ 09:26
    Dave Woestenborghs
    100

    Ah,

    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;
    

    Dave

  • keilo 568 posts 1023 karma points
    Jan 21, 2016 @ 11:04
    keilo
    1

    Many thanks Dave! Got it working, appreciated introducing nuPickers DotNet and help with the code.

    cheers!

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Jan 21, 2016 @ 10:09
    Simon Dingley
    1

    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/

  • keilo 568 posts 1023 karma points
    Jan 21, 2016 @ 11:09
    keilo
    0

    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;

    [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

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Jan 21, 2016 @ 11:23
    Simon Dingley
    1

    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:

    http://data.okfn.org/data/core/country-list

  • keilo 568 posts 1023 karma points
    Jan 21, 2016 @ 11:58
    keilo
    0

    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.

  • Simon Dingley 1470 posts 3427 karma points c-trib
    Jan 21, 2016 @ 12:02
  • Oliver 6 posts 75 karma points
    Dec 17, 2019 @ 07:00
    Oliver
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft