2

I am using the following syntax in my View to create a dropdown list from a Enum in my class. The error is

One or more compilation references are missing. Possible causes include a missing preserveCompilationContext property under buildOptions in the application's project.json.

<div class="form-group">
    <select asp-for="Subscription" class="form-control" asp-items="@new  SelectList(Enum.GetNames(typeof(SubscriptionTypes)))">
        <option disabled selected value="">Select a Subscription</option>
    </select>
</div>

The Enum in the class is:

public enum SubscriptionTypes
{
    Type1, Type2, Type3
}

3 Answers 3

3

You should use the build in generator, to generate userfriendly Values. See [Display(Name="Some Cool Name with whitespaces"] Annotation on enum values.

Html.GetEnumSelectList<CountryEnum>()

So your razor page would look like this:

@model CountryEnumViewModel

<form asp-controller="Home" asp-action="IndexEnum" method="post">
    <select asp-for="EnumCountry" 
            asp-items="Html.GetEnumSelectList<Some.NameSpace.CountryEnum>()">
    </select> 
    <br /><button type="submit">Register</button>
</form>

More info here: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-2.2#the-select-tag-helper

Sign up to request clarification or add additional context in comments.

Comments

0

Common root cause for this problem is using of non-fully qualified type names (like SubscriptionTypes) when no using directive is specified.

To fix it either add using directive at the source top:

@using Your.Namespace.Here

or use fully qualified type names:

<select asp-for="Subscription" class="form-control" asp-items="@new  SelectList(Enum.GetNames(typeof(Your.Namespace.Here.SubscriptionTypes)))">

1 Comment

I have the appropriate @using statement within my _ViewImports.cshtml file. I tried both approaches above in this specific View page but the issue remains
-1

I think this was a pretty elegant solution. I will use one of my own examples to demonstrate. I have a Model where one of the fields needs to be selected from an enumeration. The Model is called DonationMaterials and the property is Availability

public string? Availability { get; set; } = String.Empty;

This property can have 3 values; Available, Not Available, or Unknown. I created a public static method under the model called AvailabilityOptions

public static IEnumerable<SelectListItem>? AvailabilityOptions()
{
    return new[]
    {
        new  SelectListItem { Text = "Available", Value = "Available"},
        new  SelectListItem { Text = "Not Available", Value = "Not Available"},
        new  SelectListItem { Text = "Unknown", Value = "Unknown"}
    };
}

Once I created the static method, anytime I need to fill the SelectList I simply use the code in the cshtml for the page. You will need to include the Namespace of the model to have accessibility to the static method. This is clean. You could also answer the enumeration from the static method and use code to convert the enumeration into a collection of SelectListItem's

<select asp-for="DonationMaterials.Availability" asp-items="DonationMaterials.AvailabilityOptions()" class="form-control"></select>

The beauty of this approach is that the enumeration is really a Domain (mode) specific data and it keeps it where it belongs (in the model). The use of the model in the interface is quite simple.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.