VSCode, Enums and Html Helpers

Last week I took a bit of a gamble when doing a job. I had a task to do and I had two options, one option I knew could be done but it just felt wrong. The second option I thought would be possible but wasn't 100% sure. 

This blog is me talking about what I did. 

I had a change request come in from a client where a drop down menu of nationalities was to be updated from 4 options (Scottish, English, Irish, Welsh) to 160 options! 

To add a layer of complexity, these options match up to values in Dynamics CRM. 

The client emailed over a table that looked similar to : 

Scottish 100001
English 100002
Welsh 100003

...

Zimbabwean 1000160

 

The first task was to add all these values to a model which had a list of Enums - the format was Nationality and then a value which matches up with Dynamics CRM. e.g.

Scottish = 100001,
English = 100002

I fired up Visual Studio Code and pasted the table from my email to see if I could manipulate the data to work for me, good news! When I pasted the list in it pasted in with each nationality as a new line, a good start.

Nationalities 

Now I needed to add the = between the name and the value.

Regex to the rescue. In Visual Studio Code you can search by Regex.

/s : matches any whitespace characters such as space or tab

/d : matches any digit character

so using the VS Code Find/Replace, I entered `\s\d`

Find whitespace or tab until you find a digit and then replace it with =1. I'm sure there must be a way to exclude the digit when doing the replace but I didn't need to worry about it or waste too much time googling!

 

 

Next thing I needed was to add a comma at the end of every line. I did consider entering a comma in manually but where's the fun in that! 

I found that I could use regex again.

To find the end of a line, you can enter $ as the find value and this finds the end of the line and then I add the comma in to the replace value. Make sure you choose to search by regex. 

Regex Visual Studio Code

I now had a list in the correct format all I had to do was copy / paste this in to my model. 

 public enum Nationality
    {
        
        British = 100000000,
        Bulgarian = 100000003,
        Italian = 100000021,
        Polish = 100000027,
        Romanian = 100000001,
        Afghan = 100000035,

First step finished so it was time to grab a coffee. ☕

The next part I needed to update was the front end. The list of nationalities is displayed in a drop down list. 

As there were only 4 originally, the dropdown list was coded like : 

<select id="@(nameof(Model.Nationality))" name="@(nameof(Model.Nationality))" class="@(Model.ErrorList.Keys.Contains(errorKey) ? "error __inline" : string.Empty)">
                <option value="">Nationality</option>
                <option value="@(ProjectName.Core.SecureForm.Models.Nationality.English)" @(Model.Nationality == ProjectName.Core.SecureForm.Models.Nationality.English ? "selected" : string.Empty)>English</option>
                <option value="@(ProjectName.Core.SecureForm.Models.Nationality.Irish)" @(Model.Nationality == ProjectName.Core.SecureForm.Models.Nationality.Irish ? "selected" : string.Empty)>Irish</option>
                <option value="@(ProjectName.Core.SecureForm.Models.Nationality.Scottish)" @(Model.Nationality == ProjectName.Core.SecureForm.Models.Nationality.Scottish ? "selected" : string.Empty)>Scottish</option>
                <option value="@(ProjectName.Core.SecureForm.Models.Nationality.Welsh)" @(Model.Nationality == ProjectName.Core.SecureForm.Models.Nationality.Welsh ? "selected" : string.Empty)>Welsh</option>
            </select>

Now that I had 160 options, I really didn't fancy copy/paste the option tag 160 times only to change the nationality value. 

A bit of googling and testing I ended up finding a Html Helper which said it was for .Net Core but I thought I'd see if it actually worked in a none Core project - it did! 

    @Html.EnumDropDownListFor(model => model.Nationality, "Select your nationality", new {id=nameof(Model.Nationality), @class="@(Model.ErrorList.Keys.Contains(errorKey) ? 'error __inline' : string.Empty)", name=nameof(Model.Nationality)})

This creates the dropdown list and uses the enum values and names. Perfect! 

I now had 160 Nationalities in my dropdown menu and it just felt a bit cleaner. 

One side note, when I entered the nationalities in as Enums, I found that a Nationalitiy such "New Zealander" doesn't work as a Enum name due to the space, so I had to do:

[Display(Name ="New Zealander")]
NewZealander = 100000002,

This displayed New Zealander to the front end but still ties up the Enum value.

If you want to know more about Enums, the Microsoft Docs are a good place to start.

Published on: 15 November 2021