Ray
Ray

Reputation: 12441

Blazor WebAssembly pass enum as parameter to component

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?

Upvotes: 2

Views: 5603

Answers (1)

enet
enet

Reputation: 45626

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.

Upvotes: 5

Related Questions