Create a Relational Database using ASP.NET Core (MVC) Pt. 3 | Dear Coder
Tags: csharp, database, tutorial, postgres
Published: 2021-05-04 12:57:21

Dear Coder,

I have a problem.

If you read my last letter, you might recall I [used Entity Framework] (https://dev.to/piaomu/creating-a-relational-database-using-asp-net-core-mvc-pt-2-dear-coder-35h7) to save a lot of time scaffolding my Controllers and Views for the Genres and Uploads Models for my ToonSpace application.

But I signed my last letter with a minor bug:

![Blank Dropdown Bug](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jai35goa5fdrut1z9a01.png)

My GenreId dropdown (which should just display "Genre", but I'll get to that later) displays blank. This isn't a big deal though, as it's simply a result of Entity Framework trying to anticipate the purpose of that field. But it isn't functional.

I want to provide the user with a pre-populated list of options for the Genre of their uploaded webtoons, and I achieve this with _enums_.

## Using Enums to Create a Dropdown

I first create a new folder on the top level of my project in the _Solutions Explorer_ called "enums" and add a new class to it called GenreName.

![Enum Folder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3nlk8kpi6wmy3ai6kblt.png)


The _enum_ type is essentially a comma-separated list of strings, zero-indexed, and alphanumerical. I use the indexed property of my enum to function as the GenreId for each Upload object.

I declare it as a public enum instead of a class and create five options for a user to choose from.

![GenreName enum](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z36hxq651r94hymujhc6.png)

Next I add GenreName as a property of my upload Model, making sure to a using directive for GenreName in my using directives above.

![Updated Uploads Model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qeffcyccso66ryyuy8q2.png)

## Display Enum Data in Dropdown

I make some small changes to my Create and Edit Views in the Uploads View folder, changing all instances of GenreId to GenreNames and amending the asp-items property to connect it to my enum.

```
            <div class="form-group">
                <label asp-for="GenreName" class="control-label"></label>
                <select asp-for="GenreName" class ="form-control" asp-items="Html.GetEnumSelectList<GenreName>()"></select>
            </div>
```

I make sure to add a using directive to bring in my Enums folder as well.

![@using ToonSpace.Enums](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q7uigwkdk86bzti53l5m.png)

Before building the solution, I migrate my changes to the database and update it.

Finally, I build the solution and check that my new dropdown menu works.

![It works!](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yy6ipk6mcd30n7fljuwn.png)

## A brief Note on Data Annotations

If you're like me, dear Coder, you might find the Pascal case of _GenreName_ and _SciFi_ in the dropdown to be off-putting from the end user's perspective. 

We can fix these cosmetic issues by leveraging Data Annotations.

Though enums are alphanumerical, I can still use a hyphen within a data annotation to display the enum's value in the View. I simply add this using directive:

```
using System.ComponentModel.DataAnnotations;
```

and this display text above the index of my desired value:
```
[Display(Name = "Sci-Fi")]
SciFi,
```

The result is something like this on the page:
![Data Annotations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkbi1r0azsgeos5kfs6c.png)

I simply use the same using directive and display syntax in my Upload model to clean up the Genre Name tag.

![Final Upload Model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lqfte6o4012jy35y7y19.png)

---

Finally, our bugs are fixed and the data displays nicely for the end user.

![All Done](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1imxwbk8mman3e67saxr.png)

I hope my letters are finding you well, dearest Coder, and I look forward to your swift replies.

Until then, godspeed in your keystrokes.

Clickity Clacks,

Kasey