2

I'm creating a Blazor WebAssembly app, I have an enum I'd like to pass as parameter to my Blazor component like this

public enum EMenu
{
    Menu1 = 1,
    Menu2 = 2,
    Menu3 = 3,
}

[Parameter] public EMenu Id { get; set; }

<NavMenu id="menu1" />

The EMenu is my enum, Id is my parameter on my NavMenu component. But it errors out with Specified cast is not valid message.

I googled and saw this GitHub issue https://github.com/dotnet/aspnetcore/issues/19139 and according to it the following should work when you prefix the enum value with your enum name

<NavMenu id="EMenu.Menu1" />

But it did not work for me, same error. Could someone help point out what's wrong?

1 Answer 1

5

If this parameter property is defined on your NavMenu component

[Parameter] public EMenu Id { get; set; }

Then you instantiate the NavMenu component like this:

<NavMenu Id="EMenu.Menu1"/>

Id instead of id as you did... Id is a component parameter that starts with capital letter.

Your enum class should be residing in the root folder of your application.

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

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.