Search code examples
c#.net-corerazor-pagesrazorengineblazor-server-side

Set Navlink Href Property with Enum


In blazor application i am trying to set href property of NavLink with the help of Enum

Example :

 <NavLink class="nav-link" href="/Products/ProductType.All" Match="NavLinkMatch.All">
            <span class="oi oi-home" aria-hidden="true"></span> Home
 </NavLink>

Here ProductType is an Enum but in browser it make url like

ProductList/ProductTypes.All

instead of

ProductList/1

where 1 is for All

If i put @ symbol here

href="/ProductList/@ProductTypes.All"

than it say component attribute does not support complex content

what is the workaround here


Solution

  • If your enum is

    public enum ProductTypes{
       All = 1
    }
    

    so try to use this

    href="@($"/ProductList/{(int)ProductTypes.All}")"
    

    Hope it helps