1

In 2sxc App, in my content type, I have a dropdown list of Canadian provinces that have a name and a value for the abbreviation. It looks like this:

British Columbia:bc Alberta:ab Prince Edward Island:pei

Etc.

I want to be able to use both the text labels and the select value in my c# razor template. When I use @Content.Province, it only outputs the value so if I select British Columbia it outputs it as "bc". How can I output the label of the selection so that it outputs "British Columbia"?

5 Answers5

2

A minor update: I created a tutorial example showing how to access the configured dropdown properties in C#. This doesn't exactly answer the question here, but is probably useful for people looking for something like this:

https://2sxc.org/dnn-tutorials/en/razor/data910/page

iJungleBoy
  • 5,325
  • 1
  • 9
  • 21
1

I believe that those labels are only in the edit ui and cannot be pulled by token.

Please read here: 2sxc: Dropdown list Get Label instead of value in razor template

1

Though there may be a way to get at the Label from the Entity or Value, another option is available, though it would require a few steps to get there.

First you'd have to standardize your Values to match ISO and what DNN already has stored. Have a look in the dbo.Lists table in any DNN db.

SELECT * FROM [dbo].[Lists] WHERE ListName = 'Region'

So for example you'd have to have your dropdown value for Prince Edward Island be "PE"

Then you could use DNN's ListController to fetch Canada's regions, put this code in a View and it will spew the name/value pairs built in to DNN:

@using DotNetNuke.Common.Lists
@{
  IEnumerable<ListEntryInfo> listEntryInfoCol = (new ListController()).GetListEntryInfoItems("Region", "Country.CA");
}
<pre>
Value = Text:
  @foreach( var region in listEntryInfoCol ) {
<span>@region.Value = @region.Text</span>
  }
</pre>

You'd probably want to pull a Dictionary or array instead, then you could use the value as the key to get the Text you need.

So I am saying in code you'd load up your CA regions from DNN Lists, then when you want to output "Prince Edward Island" for PE, @dictRegions["PE"].

Jeremy Farrance
  • 740
  • 6
  • 11
1

Did you see my answer above? You are duplicating stuff already in DNN. I guess I never got to the point...

So take a look at this. I am not sure how to reduce the complexity of the Linq/Lambda part, but technically its just one line of code. ;)

Maybe you or someone else can simplify it. But since DNN already has all the data built in, here is how you get it in to your provinceMappings dictionary:

@using DotNetNuke.Common.Lists
@using System.Linq
@{
  Dictionary<string, string> provinceMappings = (new ListController())
    .GetListEntryInfoDictionary("Region", "Country.CA")
    .Select(x => new { x.Value.Value, x.Value.Text } )
    .ToDictionary(x => x.Value, x => x.Text );

  <pre>
  That's it, done, now you can use it like you want:
  provinceMappings["PE"] = @provinceMappings["PE"]

  Key: Value
  @foreach( var region in provinceMappings ) {
  <span>@region.Key: @region.Value</span>
  }

  Count: @provinceMappings.Count()
  Type: @provinceMappings.GetType()
  JSON: @DotNetNuke.Common.Utilities.JsonExtensionsWeb.ToJson(provinceMappings)
  </pre>
}

Put that in a view and you can see that provinceMappings has the thing you need populated from DNN, you don't have to build. Here is what the output looks like if you don't want to drop it in a View,

enter image description here

Jeremy Farrance
  • 740
  • 6
  • 11
  • Makes sense and thanks for your answer! I did see your answer but after I already implemented my solution. Also, I'm not sure if it makes any difference, but my list of Provinces and Territories isn't the full list of Canadian provinces and territories (as the organization I'm building this for only has a select list of Provinces and Territories so I already manually wrote out the list in a drodpown). – Aaron - Wolf X Machina Sep 19 '20 at 00:12
0

I ended up using this approach:

 @{
    var provinceMappings = new Dictionary<string, string>();
                provinceMappings.Add("ab", "Alberta");
                provinceMappings.Add("bc", "British Columbia");
                provinceMappings.Add("mb", "Manitoba");
                provinceMappings.Add("nb", "New Brunswick");
                provinceMappings.Add("nl", "Newfoundland &amp; Labrador");
                provinceMappings.Add("nwt", "Northwest Territories");
                provinceMappings.Add("ns", "Nova Scotia");
                provinceMappings.Add("nu", "Nunavut");
                provinceMappings.Add("on", "Ontario");
                provinceMappings.Add("pei", "Prince Edward Island");
                provinceMappings.Add("qc", "Quebec");
                provinceMappings.Add("sk", "Saskwatchewan");
                provinceMappings.Add("yt", "Yukon Territories");
    }

@provinceMappings[p]